diff --git a/api/job_events_test.go b/api/job_events_test.go index e9cf4415..d7f46bd7 100644 --- a/api/job_events_test.go +++ b/api/job_events_test.go @@ -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) @@ -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: @@ -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()