From 755fa27e39f80d877d996ca4c40ba6add88224c8 Mon Sep 17 00:00:00 2001 From: actatum Date: Mon, 10 Oct 2022 11:00:26 -0400 Subject: [PATCH] simplifying test for context deadline priority --- server_test.go | 55 +++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/server_test.go b/server_test.go index 5a30e09..525a92d 100644 --- a/server_test.go +++ b/server_test.go @@ -345,25 +345,25 @@ func TestServer_handler(t *testing.T) { subject := strconv.Itoa(rand.Int()) srv.Handle(subject, func(ctx context.Context, r Request) Response { - start := time.Now() - defer func(start time.Time) { - if time.Since(start).Round(1*time.Second) < timeout.Round(1*time.Second) { - t.Errorf("time.Since(start) got = %v, want %v", time.Since(start), timeout) - } - }(start) - ticker := time.NewTicker(timeout + 1*time.Second) - defer ticker.Stop() - for { - select { - case <-ctx.Done(): - return NewErrorResponse(r.Reply, Error{ - Code: ErrorCodeDeadlineExceeded, - Message: ctx.Err().Error(), - }) - case <-ticker.C: - return NewErrorResponse(r.Reply, fmt.Errorf("somethings wrong")) - } + dl, ok := ctx.Deadline() + if !ok { + t.Error("context should have deadline") + } + + var req map[string]time.Time + _ = r.Decode(&req) + + if req["default"].After(dl) { + t.Errorf("req[default] got = %v, want before %v", req["default"], dl) + } + + var resp Response + resp, err = NewResponse(r.Reply, map[string]string{"success": "ok"}) + if err != nil { + return NewErrorResponse(r.Reply, err) } + + return resp }) runCh := make(chan error) @@ -381,21 +381,16 @@ func TestServer_handler(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() - req, err := NewRequest(subject, map[string]string{"x": "D"}) + ctxWithDefaultServerTimeout, cancel2 := context.WithTimeout(ctx, srv.timeout) + defer cancel2() + + defaultDeadline, _ := ctxWithDefaultServerTimeout.Deadline() + + req, err := NewRequest(subject, map[string]time.Time{"default": defaultDeadline}) if err != nil { t.Fatal(err) } - resp := client.Do(ctx, req) - var e *Error - ok := errors.As(resp.Err, &e) - if !ok { - t.Fatalf("expected error to be of type Error, got %T", resp.Err) - } - if e.Code != ErrorCodeDeadlineExceeded { - t.Fatalf("e.Code got = %v, want %v", e.Code, ErrorCodeDeadlineExceeded) - } else if e.Message != context.DeadlineExceeded.Error() { - t.Fatalf("e.Message got = %v, want %v", e.Message, context.DeadlineExceeded.Error()) - } + _ = client.Do(ctx, req) if err = srv.Shutdown(ctx); err != nil { t.Fatal(err)