From 8bf979fe1c7962acd86b914a9223dbce3d05a5dc Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Thu, 20 Jun 2024 12:38:36 +0000 Subject: [PATCH] Consume events before testing The watcher uses a buffered channel, so we may get stray events that we need to consume before generating new ones in the tests. This is just for testing purposes. In actual use cases, we never expect to only have one event generated. Signed-off-by: Gabriel Adrian Samfira --- database/watcher/watcher_store_test.go | 31 ++++++++++++++++++++------ 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/database/watcher/watcher_store_test.go b/database/watcher/watcher_store_test.go index bb2a42de..b6a5bcb2 100644 --- a/database/watcher/watcher_store_test.go +++ b/database/watcher/watcher_store_test.go @@ -33,6 +33,7 @@ func (s *WatcherStoreTestSuite) TestJobWatcher() { s.Require().NoError(err) s.Require().NotNil(consumer) s.T().Cleanup(func() { consumer.Close() }) + consumeEvents(consumer) jobParams := params.Job{ ID: 1, @@ -112,12 +113,8 @@ func (s *WatcherStoreTestSuite) TestJobWatcher() { updatedJob, err = s.store.CreateOrUpdateJob(s.ctx, jobParams) s.Require().NoError(err) - select { - case <-consumer.Watch(): - // throw away event. - case <-time.After(1 * time.Second): - s.T().Fatal("unexpected payload received") - } + // We don't care about the update event here. + consumeEvents(consumer) err = s.store.BreakLockJobIsQueued(s.ctx, updatedJob.ID) s.Require().NoError(err) @@ -130,7 +127,7 @@ func (s *WatcherStoreTestSuite) TestJobWatcher() { job, ok := event.Payload.(params.Job) s.Require().True(ok) s.Require().Equal(job.ID, updatedJob.ID) - s.Require().Equal(job.LockedBy, uuid.Nil) + s.Require().Equal(uuid.Nil, job.LockedBy) case <-time.After(1 * time.Second): s.T().Fatal("expected payload not received") } @@ -148,6 +145,7 @@ func (s *WatcherStoreTestSuite) TestInstanceWatcher() { s.Require().NoError(err) s.Require().NotNil(consumer) s.T().Cleanup(func() { consumer.Close() }) + consumeEvents(consumer) ep := garmTesting.CreateDefaultGithubEndpoint(s.ctx, s.store, s.T()) creds := garmTesting.CreateTestGithubCredentials(s.ctx, "test-creds", s.store, s.T(), ep) @@ -247,6 +245,7 @@ func (s *WatcherStoreTestSuite) TestPoolWatcher() { s.Require().NoError(err) s.Require().NotNil(consumer) s.T().Cleanup(func() { consumer.Close() }) + consumeEvents(consumer) ep := garmTesting.CreateDefaultGithubEndpoint(s.ctx, s.store, s.T()) creds := garmTesting.CreateTestGithubCredentials(s.ctx, "test-creds", s.store, s.T(), ep) @@ -360,6 +359,7 @@ func (s *WatcherStoreTestSuite) TestControllerWatcher() { s.Require().NoError(err) s.Require().NotNil(consumer) s.T().Cleanup(func() { consumer.Close() }) + consumeEvents(consumer) metadataURL := "http://metadata.example.com" updateParams := params.UpdateControllerParams{ @@ -394,6 +394,7 @@ func (s *WatcherStoreTestSuite) TestEnterpriseWatcher() { s.Require().NoError(err) s.Require().NotNil(consumer) s.T().Cleanup(func() { consumer.Close() }) + consumeEvents(consumer) ep := garmTesting.CreateDefaultGithubEndpoint(s.ctx, s.store, s.T()) creds := garmTesting.CreateTestGithubCredentials(s.ctx, "test-creds", s.store, s.T(), ep) @@ -460,6 +461,7 @@ func (s *WatcherStoreTestSuite) TestOrgWatcher() { s.Require().NoError(err) s.Require().NotNil(consumer) s.T().Cleanup(func() { consumer.Close() }) + consumeEvents(consumer) ep := garmTesting.CreateDefaultGithubEndpoint(s.ctx, s.store, s.T()) creds := garmTesting.CreateTestGithubCredentials(s.ctx, "test-creds", s.store, s.T(), ep) @@ -526,6 +528,7 @@ func (s *WatcherStoreTestSuite) TestRepoWatcher() { s.Require().NoError(err) s.Require().NotNil(consumer) s.T().Cleanup(func() { consumer.Close() }) + consumeEvents(consumer) ep := garmTesting.CreateDefaultGithubEndpoint(s.ctx, s.store, s.T()) creds := garmTesting.CreateTestGithubCredentials(s.ctx, "test-creds", s.store, s.T(), ep) @@ -593,6 +596,7 @@ func (s *WatcherStoreTestSuite) TestGithubCredentialsWatcher() { s.Require().NoError(err) s.Require().NotNil(consumer) s.T().Cleanup(func() { consumer.Close() }) + consumeEvents(consumer) ghCredParams := params.CreateGithubCredentialsParams{ Name: "test-creds", @@ -667,6 +671,7 @@ func (s *WatcherStoreTestSuite) TestGithubEndpointWatcher() { s.Require().NoError(err) s.Require().NotNil(consumer) s.T().Cleanup(func() { consumer.Close() }) + consumeEvents(consumer) ghEpParams := params.CreateGithubEndpointParams{ Name: "test", @@ -726,3 +731,15 @@ func (s *WatcherStoreTestSuite) TestGithubEndpointWatcher() { s.T().Fatal("expected payload not received") } } + +func consumeEvents(consumer common.Consumer) { +consume: + for { + select { + case <-consumer.Watch(): + // throw away event. + case <-time.After(100 * time.Millisecond): + break consume + } + } +}