Merge pull request #22 from foomo/feature/update-request-queue

feat: add update request queue test
This commit is contained in:
Stefan Martinov 2020-11-23 11:34:24 +01:00 committed by GitHub
commit e0675c07f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 1 deletions

65
repo/loader_test.go Normal file
View File

@ -0,0 +1,65 @@
package repo
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
updateDuration = 5 * time.Millisecond
)
func TestRepo_tryUpdate(t *testing.T) {
repo := &Repo{
updateInProgressChannel: make(chan chan updateResponse, 1),
}
executed := make(chan time.Time, 4)
// Consumer For Updates
go func() {
for {
res := <-repo.updateInProgressChannel
now := time.Now()
executed <- now
time.Sleep(updateDuration)
res <- updateResponse{now.UnixNano(), nil}
}
}()
update := func(index int, expectError bool) {
go func() {
_, err := repo.tryUpdate()
assert.Equal(t, expectError, err != nil, "Error in request %d", index)
}()
}
// First one is being executed
update(0, false)
time.Sleep(1 * time.Millisecond)
assert.Equal(t, 1, len(executed))
// Second one is being queued
update(1, false)
time.Sleep(1 * time.Millisecond)
assert.Equal(t, 1, len(executed))
// Last one gets "Accepted"
time.Sleep(1 * time.Millisecond)
lastUpdateRequest := time.Now()
update(2, true)
assert.Equal(t, 1, len(executed))
<-executed // Dump first execution time
// Second execution should be done after given sleep time
time.Sleep(updateDuration)
assert.Equal(t, 1, len(executed))
lastUpdate := <-executed
assert.True(t, lastUpdate.After(lastUpdateRequest))
// When all requests are complete, allow queueing up again
update(3, false)
}

View File

@ -62,7 +62,7 @@ func NewRepo(server string, varDir string) *Repo {
history: newHistory(varDir),
dimensionUpdateChannel: make(chan *repoDimension),
dimensionUpdateDoneChannel: make(chan error),
updateInProgressChannel: make(chan chan updateResponse, 0),
updateInProgressChannel: make(chan chan updateResponse, 1),
}
go repo.updateRoutine()