[go: up one dir, main page]

Skip to content

Fix race condition in tests for domains API cache

Description

This MR is my naive attempt to resolve race condition in tests for GitLab domains API cache.

See explanation below:

// Lookup function
func (c *client) GetLookup(ctx context.Context, _ string) api.Lookup {
	c.bootup <- c.stats.bumpStarted()
	defer c.stats.bumpLookups() // in the `cache` this code runs in a separate goroutine

	lookup := api.Lookup{}
	if c.failure == nil {
		lookup.Name = <-c.domain // once this unblocks tests, we immediately assert before we `bumpLookups`
	} else {
		lookup.Error = c.failure
	}

	return lookup
}

// Test:
t.Run("when item is in long cache only", func(t *testing.T) {
		withTestCache(resolverConfig{}, nil, func(cache *Cache, resolver *client) {
			cache.withTestEntry(entryConfig{expired: true, retrieved: true}, func(*Entry) {
				lookup := cache.Resolve(context.Background(), "my.gitlab.com")

				require.Equal(t, "my.gitlab.com", lookup.Name)
				require.Equal(t, uint64(0), resolver.stats.getLookups())

				resolver.domain <- "my.gitlab.com" // immediately after we read from the channel we continue to the assertion below
				require.Equal(t, uint64(1), resolver.stats.getLookups())
			})
		})
	})

Closes #436 (closed)

Edited by Grzegorz Bizon

Merge request reports

Loading