Skip to content

Commit

Permalink
improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Apr 25, 2023
1 parent 45e768a commit 8c9e13c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
7 changes: 6 additions & 1 deletion subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ func (rc *responseController) setDispatchWriteDeadline() bool {
return true
}

if err := rc.SetWriteDeadline(time.Now().Add(rc.hub.dispatchTimeout)); err != nil {
deadline := time.Now().Add(rc.hub.dispatchTimeout)
if deadline.After(rc.end) {
return true
}

if err := rc.SetWriteDeadline(deadline); err != nil {
if c := rc.hub.logger.Check(zap.ErrorLevel, "Unable to set dispatch write deadline"); c != nil {
c.Write(zap.Object("subscriber", rc.subscriber), zap.Error(err))
}
Expand Down
13 changes: 6 additions & 7 deletions subscribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ func TestSubscribePrivate(t *testing.T) {
}

func TestSubscriptionEvents(t *testing.T) {
t.Skip("todo")
hub := createDummy(WithSubscriptions())

var wg sync.WaitGroup
Expand All @@ -419,7 +418,7 @@ func TestSubscriptionEvents(t *testing.T) {
defer wg.Done()
req := httptest.NewRequest(http.MethodGet, defaultHubURL+"?topic=/.well-known/mercure/subscriptions/{topic}/{subscriber}", nil).WithContext(ctx1)
req.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: createDummyAuthorizedJWT(hub, roleSubscriber, []string{"/.well-known/mercure/subscriptions/{topic}/{subscriber}"})})
w := httptest.NewRecorder()
w := newSubscribeRecorder()
hub.SubscribeHandler(w, req)

resp := w.Result()
Expand All @@ -444,15 +443,15 @@ func TestSubscriptionEvents(t *testing.T) {
defer wg.Done()
req := httptest.NewRequest(http.MethodGet, defaultHubURL+"?topic=/.well-known/mercure/subscriptions/{topicSelector}/{subscriber}", nil).WithContext(ctx2)
req.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: createDummyAuthorizedJWT(hub, roleSubscriber, []string{})})
w := httptest.NewRecorder()
w := newSubscribeRecorder()
hub.SubscribeHandler(w, req)

resp := w.Result()
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, ":\n", string(body))
assert.Equal(t, "", string(body))
}()

go func() {
Expand Down Expand Up @@ -849,8 +848,7 @@ func TestSubscribeHeartbeat(t *testing.T) {
}

func TestSubscribeExpires(t *testing.T) {
t.Skip("todo")
hub := createAnonymousDummy()
hub := createAnonymousDummy(WithWriteTimeout(0), WithDispatchTimeout(0), WithHeartbeat(500*time.Millisecond))
token := jwt.New(jwt.SigningMethodHS256)

token.Claims = &claims{
Expand All @@ -866,13 +864,14 @@ func TestSubscribeExpires(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, defaultHubURL+"?topic=foo", nil)
req.Header.Add("Authorization", "Bearer "+jwt)

w := httptest.NewRecorder()
w := newSubscribeRecorder()
hub.SubscribeHandler(w, req)

resp := w.Result()
defer resp.Body.Close()

assert.Equal(t, 200, resp.StatusCode)
assert.True(t, time.Now().After(token.Claims.(*claims).ExpiresAt.Time))
}

func BenchmarkSubscribe(b *testing.B) {
Expand Down

0 comments on commit 8c9e13c

Please sign in to comment.