Skip to content

Commit

Permalink
Merge pull request #19 from make-software/hotfix/fix_worker_count_loop
Browse files Browse the repository at this point in the history
Fix the worker count loop logic
  • Loading branch information
koltsov-iv authored Jun 12, 2023
2 parents 88dafeb + cba0701 commit b9ebbce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sse/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (p *Client) Start(ctx context.Context, lastEventID int) error {
groupErrs.Go(func() error {
return p.Streamer.FillStream(ctx, lastEventID, p.EventStream, p.streamErrors)
})
for i := 1; i < p.WorkersCount; i++ {
for i := 0; i < p.WorkersCount; i++ {
newCtx := context.WithValue(ctx, CtxWorkerIDKey, i)
groupErrs.Go(func() error {
return p.Consumer.Run(newCtx, p.EventStream, p.consumerErrors)
Expand Down
16 changes: 16 additions & 0 deletions tests/sse/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,21 @@ func Test_HttpConnection_request(t *testing.T) {
return nil
})
assert.Error(t, client.Start(context.Background(), 123))
}

func Test_withOneWorker_shouldProcessRequest(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
_, err := writer.Write(json.RawMessage(`data: {"ApiVersion":"1.0.0"}`))
require.NoError(t, err)
}))

client := sse.NewClient(server.URL)
client.WorkersCount = 1
var result bool
client.RegisterHandler(sse.APIVersionEventType, func(ctx context.Context, event sse.RawEvent) error {
result = true
return nil
})
assert.Error(t, client.Start(context.Background(), 0))
assert.True(t, result)
}

0 comments on commit b9ebbce

Please sign in to comment.