Fix data races
parent
9202d85532
commit
8c32f029fb
|
@ -25,6 +25,7 @@ and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/release
|
|||
**Documentation**:
|
||||
|
||||
* [Examples](examples.md) for [Home Assistant](https://www.home-assistant.io/) ([#282](https://github.com/binwiederhier/ntfy/pull/282), thanks to [@poblabs](https://github.com/poblabs))
|
||||
* Install instructions for [NixOS/Nix](https://ntfy.sh/docs/install/#nixos-nix) ([#282](https://github.com/binwiederhier/ntfy/pull/282), thanks to [@arjan-s](https://github.com/arjan-s))
|
||||
|
||||
-->
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
"heckel.io/ntfy/auth"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -29,6 +30,7 @@ func (t testAuther) Authorize(_ *auth.User, _ string, _ auth.Permission) error {
|
|||
type testFirebaseSender struct {
|
||||
allowed int
|
||||
messages []*messaging.Message
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func newTestFirebaseSender(allowed int) *testFirebaseSender {
|
||||
|
@ -37,7 +39,10 @@ func newTestFirebaseSender(allowed int) *testFirebaseSender {
|
|||
messages: make([]*messaging.Message, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *testFirebaseSender) Send(m *messaging.Message) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if len(s.messages)+1 > s.allowed {
|
||||
return errFirebaseQuotaExceeded
|
||||
}
|
||||
|
@ -45,6 +50,16 @@ func (s *testFirebaseSender) Send(m *messaging.Message) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *testFirebaseSender) Messages() []*messaging.Message {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
messages := make([]*messaging.Message, 0)
|
||||
for _, m := range s.messages {
|
||||
messages = append(messages, m)
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
||||
func TestToFirebaseMessage_Keepalive(t *testing.T) {
|
||||
m := newKeepaliveMessage("mytopic")
|
||||
fbm, err := toFirebaseMessage(m, nil)
|
||||
|
@ -311,15 +326,15 @@ func TestToFirebaseSender_Abuse(t *testing.T) {
|
|||
visitor := newVisitor(newTestConfig(t), newMemTestCache(t), "1.2.3.4")
|
||||
|
||||
require.Nil(t, client.Send(visitor, &message{Topic: "mytopic"}))
|
||||
require.Equal(t, 1, len(sender.messages))
|
||||
require.Equal(t, 1, len(sender.Messages()))
|
||||
|
||||
require.Nil(t, client.Send(visitor, &message{Topic: "mytopic"}))
|
||||
require.Equal(t, 2, len(sender.messages))
|
||||
require.Equal(t, 2, len(sender.Messages()))
|
||||
|
||||
require.Equal(t, errFirebaseQuotaExceeded, client.Send(visitor, &message{Topic: "mytopic"}))
|
||||
require.Equal(t, 2, len(sender.messages))
|
||||
require.Equal(t, 2, len(sender.Messages()))
|
||||
|
||||
sender.messages = make([]*messaging.Message, 0) // Reset to test that time limit is working
|
||||
require.Equal(t, errFirebaseQuotaExceeded, client.Send(visitor, &message{Topic: "mytopic"}))
|
||||
require.Equal(t, 0, len(sender.messages))
|
||||
require.Equal(t, 0, len(sender.Messages()))
|
||||
}
|
||||
|
|
|
@ -63,10 +63,10 @@ func TestServer_PublishWithFirebase(t *testing.T) {
|
|||
msg1 := toMessage(t, response.Body.String())
|
||||
require.NotEmpty(t, msg1.ID)
|
||||
require.Equal(t, "my first message", msg1.Message)
|
||||
require.Equal(t, 1, len(sender.messages))
|
||||
require.Equal(t, "my first message", sender.messages[0].Data["message"])
|
||||
require.Equal(t, "my first message", sender.messages[0].APNS.Payload.Aps.Alert.Body)
|
||||
require.Equal(t, "my first message", sender.messages[0].APNS.Payload.CustomData["message"])
|
||||
require.Equal(t, 1, len(sender.Messages()))
|
||||
require.Equal(t, "my first message", sender.Messages()[0].Data["message"])
|
||||
require.Equal(t, "my first message", sender.Messages()[0].APNS.Payload.Aps.Alert.Body)
|
||||
require.Equal(t, "my first message", sender.Messages()[0].APNS.Payload.CustomData["message"])
|
||||
}
|
||||
|
||||
func TestServer_SubscribeOpenAndKeepalive(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue