mirror of
https://github.com/elyby/chrly.git
synced 2025-01-03 18:51:49 +05:30
Fix FullBus test
This commit is contained in:
parent
d8f6786c69
commit
b2e501af60
@ -2,6 +2,7 @@ package mojangtextures
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
@ -60,10 +61,10 @@ func (o *mojangUsernamesToUuidsRequestMock) UsernamesToUuids(usernames []string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
type manualStrategy struct {
|
type manualStrategy struct {
|
||||||
ch chan *JobsIteration
|
ch chan *JobsIteration
|
||||||
once sync.Once
|
once sync.Once
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
jobs []*job
|
jobs []*job
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *manualStrategy) Queue(job *job) {
|
func (m *manualStrategy) Queue(job *job) {
|
||||||
@ -104,7 +105,7 @@ type batchUuidsProviderTestSuite struct {
|
|||||||
Strategy *manualStrategy
|
Strategy *manualStrategy
|
||||||
MojangApi *mojangUsernamesToUuidsRequestMock
|
MojangApi *mojangUsernamesToUuidsRequestMock
|
||||||
|
|
||||||
GetUuidAsync func(username string) <- chan *batchUuidsProviderGetUuidResult
|
GetUuidAsync func(username string) <-chan *batchUuidsProviderGetUuidResult
|
||||||
stop context.CancelFunc
|
stop context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,7 +119,7 @@ func (suite *batchUuidsProviderTestSuite) SetupTest() {
|
|||||||
|
|
||||||
suite.Provider = NewBatchUuidsProvider(ctx, suite.Strategy, suite.Emitter)
|
suite.Provider = NewBatchUuidsProvider(ctx, suite.Strategy, suite.Emitter)
|
||||||
|
|
||||||
suite.GetUuidAsync = func(username string) <- chan *batchUuidsProviderGetUuidResult {
|
suite.GetUuidAsync = func(username string) <-chan *batchUuidsProviderGetUuidResult {
|
||||||
s := make(chan struct{})
|
s := make(chan struct{})
|
||||||
// This dirty hack ensures, that the username will be queued before we return control to the caller.
|
// This dirty hack ensures, that the username will be queued before we return control to the caller.
|
||||||
// It's needed to keep expected calls order and prevent cases when iteration happens before
|
// It's needed to keep expected calls order and prevent cases when iteration happens before
|
||||||
@ -198,7 +199,7 @@ func (suite *batchUuidsProviderTestSuite) TestShouldNotSendRequestWhenNoJobsAreR
|
|||||||
_ = suite.GetUuidAsync("username") // Schedule one username to run the queue
|
_ = suite.GetUuidAsync("username") // Schedule one username to run the queue
|
||||||
|
|
||||||
suite.Strategy.Iterate(0, 1) // Return no jobs and indicate that there is one job in queue
|
suite.Strategy.Iterate(0, 1) // Return no jobs and indicate that there is one job in queue
|
||||||
<- done
|
<-done
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test written for multiple usernames to ensure that the error
|
// Test written for multiple usernames to ensure that the error
|
||||||
@ -240,7 +241,7 @@ func TestPeriodicStrategy(t *testing.T) {
|
|||||||
iteration := <-ch
|
iteration := <-ch
|
||||||
durationBeforeResult := time.Now().Sub(startedAt)
|
durationBeforeResult := time.Now().Sub(startedAt)
|
||||||
require.True(t, durationBeforeResult >= d)
|
require.True(t, durationBeforeResult >= d)
|
||||||
require.True(t, durationBeforeResult < d * 2)
|
require.True(t, durationBeforeResult < d*2)
|
||||||
|
|
||||||
require.Equal(t, []*job{j}, iteration.Jobs)
|
require.Equal(t, []*job{j}, iteration.Jobs)
|
||||||
require.Equal(t, 0, iteration.Queue)
|
require.Equal(t, 0, iteration.Queue)
|
||||||
@ -313,7 +314,7 @@ func TestPeriodicStrategy(t *testing.T) {
|
|||||||
iteration := <-ch
|
iteration := <-ch
|
||||||
durationBeforeResult := time.Now().Sub(startedAt)
|
durationBeforeResult := time.Now().Sub(startedAt)
|
||||||
require.True(t, durationBeforeResult >= d)
|
require.True(t, durationBeforeResult >= d)
|
||||||
require.True(t, durationBeforeResult < d * 2)
|
require.True(t, durationBeforeResult < d*2)
|
||||||
|
|
||||||
require.Empty(t, iteration.Jobs)
|
require.Empty(t, iteration.Jobs)
|
||||||
require.Equal(t, 0, iteration.Queue)
|
require.Equal(t, 0, iteration.Queue)
|
||||||
@ -328,7 +329,6 @@ func TestPeriodicStrategy(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestFullBusStrategy(t *testing.T) {
|
func TestFullBusStrategy(t *testing.T) {
|
||||||
t.Run("should provide iteration immediately when the batch size exceeded", func(t *testing.T) {
|
t.Run("should provide iteration immediately when the batch size exceeded", func(t *testing.T) {
|
||||||
jobs := make([]*job, 10)
|
jobs := make([]*job, 10)
|
||||||
@ -373,18 +373,19 @@ func TestFullBusStrategy(t *testing.T) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
ch := strategy.GetJobs(ctx)
|
ch := strategy.GetJobs(ctx)
|
||||||
|
|
||||||
|
var startedAt time.Time
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
defer close(done)
|
defer close(done)
|
||||||
startedAt := time.Now()
|
|
||||||
iteration := <-ch
|
iteration := <-ch
|
||||||
duration := time.Now().Sub(startedAt)
|
duration := time.Now().Sub(startedAt)
|
||||||
require.True(t, duration >= d)
|
require.True(t, duration >= d, fmt.Sprintf("has %d, expected %d", duration, d))
|
||||||
require.True(t, duration < d * 2)
|
require.True(t, duration < d*2)
|
||||||
require.Equal(t, jobs, iteration.Jobs)
|
require.Equal(t, jobs, iteration.Jobs)
|
||||||
require.Equal(t, 0, iteration.Queue)
|
require.Equal(t, 0, iteration.Queue)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
startedAt = time.Now()
|
||||||
for _, j := range jobs {
|
for _, j := range jobs {
|
||||||
strategy.Queue(j)
|
strategy.Queue(j)
|
||||||
}
|
}
|
||||||
@ -406,7 +407,7 @@ func TestFullBusStrategy(t *testing.T) {
|
|||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
time.Sleep(5 * time.Millisecond) // See comment below
|
time.Sleep(5 * time.Millisecond) // See comment below
|
||||||
select {
|
select {
|
||||||
case iteration := <- ch:
|
case iteration := <-ch:
|
||||||
require.Len(t, iteration.Jobs, 10)
|
require.Len(t, iteration.Jobs, 10)
|
||||||
// Don't assert iteration.Queue length since it might be unstable
|
// Don't assert iteration.Queue length since it might be unstable
|
||||||
// Don't call iteration.Done()
|
// Don't call iteration.Done()
|
||||||
@ -423,7 +424,7 @@ func TestFullBusStrategy(t *testing.T) {
|
|||||||
iteration := <-ch
|
iteration := <-ch
|
||||||
duration := time.Now().Sub(startedAt)
|
duration := time.Now().Sub(startedAt)
|
||||||
require.True(t, duration >= d)
|
require.True(t, duration >= d)
|
||||||
require.True(t, duration < d * 2)
|
require.True(t, duration < d*2)
|
||||||
require.Len(t, iteration.Jobs, 1)
|
require.Len(t, iteration.Jobs, 1)
|
||||||
require.Equal(t, 0, iteration.Queue)
|
require.Equal(t, 0, iteration.Queue)
|
||||||
}()
|
}()
|
||||||
|
Loading…
Reference in New Issue
Block a user