mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-12-23 02:02:33 +01:00
Fix data races
This commit is contained in:
parent
9202d85532
commit
8c32f029fb
3 changed files with 24 additions and 8 deletions
|
@ -25,6 +25,7 @@ and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/release
|
||||||
**Documentation**:
|
**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))
|
* [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"
|
"github.com/stretchr/testify/require"
|
||||||
"heckel.io/ntfy/auth"
|
"heckel.io/ntfy/auth"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,6 +30,7 @@ func (t testAuther) Authorize(_ *auth.User, _ string, _ auth.Permission) error {
|
||||||
type testFirebaseSender struct {
|
type testFirebaseSender struct {
|
||||||
allowed int
|
allowed int
|
||||||
messages []*messaging.Message
|
messages []*messaging.Message
|
||||||
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTestFirebaseSender(allowed int) *testFirebaseSender {
|
func newTestFirebaseSender(allowed int) *testFirebaseSender {
|
||||||
|
@ -37,7 +39,10 @@ func newTestFirebaseSender(allowed int) *testFirebaseSender {
|
||||||
messages: make([]*messaging.Message, 0),
|
messages: make([]*messaging.Message, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *testFirebaseSender) Send(m *messaging.Message) error {
|
func (s *testFirebaseSender) Send(m *messaging.Message) error {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
if len(s.messages)+1 > s.allowed {
|
if len(s.messages)+1 > s.allowed {
|
||||||
return errFirebaseQuotaExceeded
|
return errFirebaseQuotaExceeded
|
||||||
}
|
}
|
||||||
|
@ -45,6 +50,16 @@ func (s *testFirebaseSender) Send(m *messaging.Message) error {
|
||||||
return nil
|
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) {
|
func TestToFirebaseMessage_Keepalive(t *testing.T) {
|
||||||
m := newKeepaliveMessage("mytopic")
|
m := newKeepaliveMessage("mytopic")
|
||||||
fbm, err := toFirebaseMessage(m, nil)
|
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")
|
visitor := newVisitor(newTestConfig(t), newMemTestCache(t), "1.2.3.4")
|
||||||
|
|
||||||
require.Nil(t, client.Send(visitor, &message{Topic: "mytopic"}))
|
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.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, 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
|
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, 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())
|
msg1 := toMessage(t, response.Body.String())
|
||||||
require.NotEmpty(t, msg1.ID)
|
require.NotEmpty(t, msg1.ID)
|
||||||
require.Equal(t, "my first message", msg1.Message)
|
require.Equal(t, "my first message", msg1.Message)
|
||||||
require.Equal(t, 1, len(sender.messages))
|
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].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.Aps.Alert.Body)
|
||||||
require.Equal(t, "my first message", sender.messages[0].APNS.Payload.CustomData["message"])
|
require.Equal(t, "my first message", sender.Messages()[0].APNS.Payload.CustomData["message"])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServer_SubscribeOpenAndKeepalive(t *testing.T) {
|
func TestServer_SubscribeOpenAndKeepalive(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue