Skip to content

Commit

Permalink
ci: debug flaky test
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Adler <[email protected]>
  • Loading branch information
michaeladler committed Sep 8, 2023
1 parent 2a9ab67 commit 2de5dd3
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions api/job_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,32 @@ func TestJobEventsSubscribe(t *testing.T) {
handler := handlers[i]
t.Run(name, func(t *testing.T) {
var jobID string
{
job, err := job.CreateJob(context.Background(), db,
&model.JobRequest{ClientID: "foo", Workflow: wf.Name})
require.NoError(t, err)
jobID = job.ID
}
job, err := job.CreateJob(context.Background(), db,
&model.JobRequest{ClientID: "foo", Workflow: wf.Name})
require.NoError(t, err)
jobID = job.ID
require.NotEmpty(t, jobID)

var wg sync.WaitGroup
ch, _ := events.AddSubscriber(context.Background(), events.FilterParams{IDs: []string{jobID}})
wg.Add(1)
go func() {
defer wg.Done()

ch, _ := events.AddSubscriber(context.Background(), events.FilterParams{IDs: []string{jobID}})
// wait for event created by our status.Update
// wait for event created by our status.Update below
<-ch
// now our GET request should have received the response as well,
// add some extra time to be safe
time.Sleep(100 * time.Millisecond)
time.Sleep(200 * time.Millisecond)
events.ShutdownSubscribers()
}()

wg.Add(1)
go func() {
defer wg.Done()
// wait for subscriber which is created by our GET request below and our test goroutine
// wait for subscriber which is created by our GET request below and our test goroutine above
for events.SubscriberCount() != 2 {
time.Sleep(50 * time.Millisecond)
time.Sleep(10 * time.Millisecond)
}
// update job
_, err = status.Update(context.Background(), db, jobID, &model.JobStatus{State: "INSTALLING"}, model.EligibleEnumCLIENT)
Expand All @@ -95,6 +93,12 @@ func TestJobEventsSubscribe(t *testing.T) {
require.NotEmpty(t, body)

lines := strings.Split(body, "\n")

t.Log("HTTP resonse body:")
for _, line := range lines {
t.Logf(">> %s", line)
}

assert.Len(t, lines, 4)

// check body starts with data:
Expand All @@ -104,9 +108,10 @@ func TestJobEventsSubscribe(t *testing.T) {
var ev events.JobEvent
err = json.Unmarshal([]byte(strings.TrimPrefix(lines[0], "data: ")), &ev)
require.NoError(t, err)
assert.Equal(t, events.ActionUpdateStatus, ev.Action)
assert.Equal(t, "INSTALLING", ev.Job.Status.State)
assert.Equal(t, wf.Name, ev.Job.Workflow.Name)

assert.Equal(t, job.ClientID, ev.Job.ClientID)
assert.Equal(t, "id: 1", lines[1])

wg.Wait()
Expand Down

0 comments on commit 2de5dd3

Please sign in to comment.