mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-12-14 11:47:33 +00:00
Fix race in tests
This commit is contained in:
parent
497f871447
commit
e147a41f92
1 changed files with 13 additions and 7 deletions
|
@ -1,40 +1,46 @@
|
||||||
package util_test
|
package util_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"heckel.io/ntfy/util"
|
"heckel.io/ntfy/util"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBatchingQueue_InfTimeout(t *testing.T) {
|
func TestBatchingQueue_InfTimeout(t *testing.T) {
|
||||||
q := util.NewBatchingQueue[int](25, 1*time.Hour)
|
q := util.NewBatchingQueue[int](25, 1*time.Hour)
|
||||||
batches := make([][]int, 0)
|
batches, total := make([][]int, 0), 0
|
||||||
total := 0
|
var mu sync.Mutex
|
||||||
go func() {
|
go func() {
|
||||||
for batch := range q.Dequeue() {
|
for batch := range q.Dequeue() {
|
||||||
|
mu.Lock()
|
||||||
batches = append(batches, batch)
|
batches = append(batches, batch)
|
||||||
total += len(batch)
|
total += len(batch)
|
||||||
|
mu.Unlock()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
for i := 0; i < 101; i++ {
|
for i := 0; i < 101; i++ {
|
||||||
go q.Enqueue(i)
|
go q.Enqueue(i)
|
||||||
}
|
}
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
mu.Lock()
|
||||||
require.Equal(t, 100, total) // One is missing, stuck in the last batch!
|
require.Equal(t, 100, total) // One is missing, stuck in the last batch!
|
||||||
require.Equal(t, 4, len(batches))
|
require.Equal(t, 4, len(batches))
|
||||||
|
mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBatchingQueue_WithTimeout(t *testing.T) {
|
func TestBatchingQueue_WithTimeout(t *testing.T) {
|
||||||
q := util.NewBatchingQueue[int](25, 100*time.Millisecond)
|
q := util.NewBatchingQueue[int](25, 100*time.Millisecond)
|
||||||
batches := make([][]int, 0)
|
batches, total := make([][]int, 0), 0
|
||||||
total := 0
|
var mu sync.Mutex
|
||||||
go func() {
|
go func() {
|
||||||
for batch := range q.Dequeue() {
|
for batch := range q.Dequeue() {
|
||||||
|
mu.Lock()
|
||||||
batches = append(batches, batch)
|
batches = append(batches, batch)
|
||||||
total += len(batch)
|
total += len(batch)
|
||||||
|
mu.Unlock()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
for i := 0; i < 101; i++ {
|
for i := 0; i < 101; i++ {
|
||||||
|
@ -44,9 +50,9 @@ func TestBatchingQueue_WithTimeout(t *testing.T) {
|
||||||
}(i)
|
}(i)
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
fmt.Println(len(batches))
|
mu.Lock()
|
||||||
fmt.Println(batches)
|
|
||||||
require.Equal(t, 101, total)
|
require.Equal(t, 101, total)
|
||||||
require.True(t, len(batches) > 4) // 101/25
|
require.True(t, len(batches) > 4) // 101/25
|
||||||
require.True(t, len(batches) < 21)
|
require.True(t, len(batches) < 21)
|
||||||
|
mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue