mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-12-23 02:02:33 +01:00
Fix test and retry
This commit is contained in:
parent
9eb94a565d
commit
c997e4911a
2 changed files with 10 additions and 19 deletions
|
@ -45,24 +45,16 @@ func newTopic(id string) *topic {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe subscribes to this topic
|
// Subscribe subscribes to this topic
|
||||||
func (t *topic) Subscribe(s subscriber, userID string, cancel func()) int {
|
func (t *topic) Subscribe(s subscriber, userID string, cancel func()) (subscriberID int) {
|
||||||
max_retries := 5
|
|
||||||
retries := 1
|
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
|
for i := 0; i < 5; i++ { // Best effort retry
|
||||||
subscriberID := rand.Int()
|
subscriberID = rand.Int()
|
||||||
// simple check for existing id in maps
|
_, exists := t.subscribers[subscriberID]
|
||||||
for {
|
if !exists {
|
||||||
_, ok := t.subscribers[subscriberID]
|
|
||||||
if ok && retries <= max_retries {
|
|
||||||
subscriberID = rand.Int()
|
|
||||||
retries++
|
|
||||||
} else {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t.subscribers[subscriberID] = &topicSubscriber{
|
t.subscribers[subscriberID] = &topicSubscriber{
|
||||||
userID: userID, // May be empty
|
userID: userID, // May be empty
|
||||||
subscriber: s,
|
subscriber: s,
|
||||||
|
|
|
@ -42,12 +42,11 @@ func TestTopic_Keepalive(t *testing.T) {
|
||||||
require.True(t, to.LastAccess().Unix() <= time.Now().Unix()+2)
|
require.True(t, to.LastAccess().Unix() <= time.Now().Unix()+2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTopic_Subscribe_duplicateID(t *testing.T) {
|
func TestTopic_Subscribe_DuplicateID(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
to := newTopic("mytopic")
|
to := newTopic("mytopic")
|
||||||
|
|
||||||
// fix random seed to force same number generation
|
// Fix random seed to force same number generation
|
||||||
rand.Seed(1)
|
rand.Seed(1)
|
||||||
a := rand.Int()
|
a := rand.Int()
|
||||||
to.subscribers[a] = &topicSubscriber{
|
to.subscribers[a] = &topicSubscriber{
|
||||||
|
@ -60,11 +59,11 @@ func TestTopic_Subscribe_duplicateID(t *testing.T) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// force rand.Int to generate the same id once more
|
// Force rand.Int to generate the same id once more
|
||||||
rand.Seed(1)
|
rand.Seed(1)
|
||||||
id := to.Subscribe(subFn, "b", func() {})
|
id := to.Subscribe(subFn, "b", func() {})
|
||||||
res := to.subscribers[id]
|
res := to.subscribers[id]
|
||||||
|
|
||||||
require.False(t, id == a)
|
require.NotEqual(t, id, a)
|
||||||
require.True(t, res.userID == "b")
|
require.Equal(t, "b", res.userID, "b")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue