Skip to content

Commit

Permalink
fix: flaky tests on the configuration watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Nov 24, 2022
1 parent 46c2666 commit 580e7fa
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions pkg/server/configurationwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (p *mockProvider) Init() error {

func TestNewConfigurationWatcher(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()
t.Cleanup(routinesPool.Stop)

pvd := &mockProvider{
messages: []dynamic.Message{{
Expand Down Expand Up @@ -115,7 +115,6 @@ func TestNewConfigurationWatcher(t *testing.T) {

func TestWaitForRequiredProvider(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

pvdAggregator := &mockProvider{
wait: 5 * time.Millisecond,
Expand Down Expand Up @@ -151,7 +150,9 @@ func TestWaitForRequiredProvider(t *testing.T) {
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

// give some time so that the configuration can be processed
time.Sleep(20 * time.Millisecond)
Expand All @@ -162,7 +163,6 @@ func TestWaitForRequiredProvider(t *testing.T) {

func TestIgnoreTransientConfiguration(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

config := &dynamic.Configuration{
HTTP: th.BuildConfiguration(
Expand Down Expand Up @@ -190,7 +190,9 @@ func TestIgnoreTransientConfiguration(t *testing.T) {
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

watcher.allProvidersConfigs <- dynamic.Message{
ProviderName: "mock",
Expand Down Expand Up @@ -243,7 +245,6 @@ func TestIgnoreTransientConfiguration(t *testing.T) {

func TestListenProvidersThrottleProviderConfigReload(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

pvd := &mockProvider{
wait: 10 * time.Millisecond,
Expand Down Expand Up @@ -274,7 +275,9 @@ func TestListenProvidersThrottleProviderConfigReload(t *testing.T) {
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

// Give some time so that the configuration can be processed.
time.Sleep(100 * time.Millisecond)
Expand All @@ -287,7 +290,6 @@ func TestListenProvidersThrottleProviderConfigReload(t *testing.T) {

func TestListenProvidersSkipsEmptyConfigs(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

pvd := &mockProvider{
messages: []dynamic.Message{{ProviderName: "mock"}},
Expand All @@ -299,15 +301,16 @@ func TestListenProvidersSkipsEmptyConfigs(t *testing.T) {
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

// give some time so that the configuration can be processed
time.Sleep(100 * time.Millisecond)
}

func TestListenProvidersSkipsSameConfigurationForProvider(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

message := dynamic.Message{
ProviderName: "mock",
Expand All @@ -331,7 +334,9 @@ func TestListenProvidersSkipsSameConfigurationForProvider(t *testing.T) {
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

// give some time so that the configuration can be processed
time.Sleep(100 * time.Millisecond)
Expand All @@ -340,7 +345,6 @@ func TestListenProvidersSkipsSameConfigurationForProvider(t *testing.T) {

func TestListenProvidersDoesNotSkipFlappingConfiguration(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

configuration := &dynamic.Configuration{
HTTP: th.BuildConfiguration(
Expand Down Expand Up @@ -374,7 +378,9 @@ func TestListenProvidersDoesNotSkipFlappingConfiguration(t *testing.T) {
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

// give some time so that the configuration can be processed
time.Sleep(100 * time.Millisecond)
Expand Down Expand Up @@ -407,7 +413,6 @@ func TestListenProvidersDoesNotSkipFlappingConfiguration(t *testing.T) {

func TestListenProvidersIgnoreSameConfig(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

configuration := &dynamic.Configuration{
HTTP: th.BuildConfiguration(
Expand Down Expand Up @@ -453,8 +458,7 @@ func TestListenProvidersIgnoreSameConfig(t *testing.T) {
configurationReloads++
lastConfig = conf

// Allows next configurations to be sent by the mock provider
// as soon as the first configuration message is applied.
// Allows next configurations to be sent by the mock provider as soon as the first configuration message is applied.
once.Do(func() {
pvd.first <- struct{}{}
// Wait for all configuration messages to pile in
Expand All @@ -463,7 +467,9 @@ func TestListenProvidersIgnoreSameConfig(t *testing.T) {
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

// Wait long enough
time.Sleep(50 * time.Millisecond)
Expand Down Expand Up @@ -498,7 +504,6 @@ func TestListenProvidersIgnoreSameConfig(t *testing.T) {

func TestApplyConfigUnderStress(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

watcher := NewConfigurationWatcher(routinesPool, &mockProvider{}, []string{"defaultEP"}, "")

Expand All @@ -525,23 +530,23 @@ func TestApplyConfigUnderStress(t *testing.T) {
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

time.Sleep(100 * time.Millisecond)

// Ensure that at least two configurations have been applied
// if we simulate being spammed configuration changes by the
// provider(s).
// In theory, checking at least one would be sufficient, but
// checking for two also ensures that we're looping properly,
// if we simulate being spammed configuration changes by the provider(s).
// In theory, checking at least one would be sufficient,
// but checking for two also ensures that we're looping properly,
// and that the whole algo holds, etc.
t.Log(configurationReloads)
assert.GreaterOrEqual(t, configurationReloads, 2)
}

func TestListenProvidersIgnoreIntermediateConfigs(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

configuration := &dynamic.Configuration{
HTTP: th.BuildConfiguration(
Expand Down Expand Up @@ -596,7 +601,9 @@ func TestListenProvidersIgnoreIntermediateConfigs(t *testing.T) {
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

// Wait long enough
time.Sleep(500 * time.Millisecond)
Expand Down Expand Up @@ -631,7 +638,6 @@ func TestListenProvidersIgnoreIntermediateConfigs(t *testing.T) {

func TestListenProvidersPublishesConfigForEachProvider(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

configuration := &dynamic.Configuration{
HTTP: th.BuildConfiguration(
Expand All @@ -656,7 +662,9 @@ func TestListenProvidersPublishesConfigForEachProvider(t *testing.T) {
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

// give some time so that the configuration can be processed
time.Sleep(100 * time.Millisecond)
Expand Down Expand Up @@ -695,7 +703,6 @@ func TestListenProvidersPublishesConfigForEachProvider(t *testing.T) {

func TestPublishConfigUpdatedByProvider(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

pvdConfiguration := dynamic.Configuration{
TCP: &dynamic.TCPConfiguration{
Expand Down Expand Up @@ -725,12 +732,14 @@ func TestPublishConfigUpdatedByProvider(t *testing.T) {
watcher.AddListener(func(configuration dynamic.Configuration) {
publishedConfigCount++

// Update the provider configuration published in next dynamic Message which should trigger a new publish.
// Update the provider configuration published in next dynamic Message which should trigger a new publishing.
pvdConfiguration.TCP.Routers["bar"] = &dynamic.TCPRouter{}
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

// give some time so that the configuration can be processed.
time.Sleep(100 * time.Millisecond)
Expand All @@ -740,7 +749,6 @@ func TestPublishConfigUpdatedByProvider(t *testing.T) {

func TestPublishConfigUpdatedByConfigWatcherListener(t *testing.T) {
routinesPool := safe.NewPool(context.Background())
defer routinesPool.Stop()

pvd := &mockProvider{
wait: 10 * time.Millisecond,
Expand Down Expand Up @@ -774,13 +782,15 @@ func TestPublishConfigUpdatedByConfigWatcherListener(t *testing.T) {
watcher.AddListener(func(configuration dynamic.Configuration) {
publishedConfigCount++

// Modify the provided configuration. This should not modify the configuration stored in the configuration
// watcher and cause a new publish.
// Modify the provided configuration.
// This should not modify the configuration stored in the configuration watcher and therefore there will be no new publishing.
configuration.TCP.Routers["foo@mock"].Rule = "bar"
})

watcher.Start()
defer watcher.Stop()

t.Cleanup(watcher.Stop)
t.Cleanup(routinesPool.Stop)

// give some time so that the configuration can be processed.
time.Sleep(100 * time.Millisecond)
Expand Down

0 comments on commit 580e7fa

Please sign in to comment.