89 lines
2.8 KiB
Go
89 lines
2.8 KiB
Go
|
package apicontext
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func TestContextIsDone(t *testing.T) {
|
||
|
ctx := WithCancel(Background())
|
||
|
defer ctx.Cancel()
|
||
|
if ContextIsDone(ctx) {
|
||
|
t.Fatalf("error: context was done before cancel()\n")
|
||
|
} else if err := ctx.Err(); err != nil {
|
||
|
t.Fatalf("error: context reporting error %v; expected nil\n", err)
|
||
|
}
|
||
|
ctx.Cancel()
|
||
|
if !ContextIsDone(ctx) {
|
||
|
t.Fatalf("error: context was not done after cancel()\n")
|
||
|
} else if want, got := context.Canceled, ctx.Err(); !errors.Is(got, want) {
|
||
|
t.Fatalf("error: wanted %v; got %v\n", want, got)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestContextWithTimeout(t *testing.T) {
|
||
|
timeout := time.Second
|
||
|
controlCtx, cancelControlCtx := context.WithTimeout(Background(), timeout*2)
|
||
|
defer cancelControlCtx()
|
||
|
ctx := WithTimeout(Background(), timeout)
|
||
|
defer ctx.Cancel()
|
||
|
if want, got := true, ctx.IsNotDone(); want != got {
|
||
|
t.Fatalf("error: wanted %v; got %v\n", want, got)
|
||
|
} else if want, got := error(nil), ctx.Err(); want != got {
|
||
|
t.Fatalf("error: wanted err == %v; got %v\n", want, got)
|
||
|
}
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
case <-controlCtx.Done():
|
||
|
t.Fatalf("error: control timeout reached - apicontext.WithTimeout failed to fire\n")
|
||
|
}
|
||
|
if want, got := true, ctx.IsDone(); want != got {
|
||
|
t.Fatalf("error: wanted %v; got %v\n", want, got)
|
||
|
} else if want, got := context.DeadlineExceeded, ctx.Err(); !errors.Is(got, want) {
|
||
|
t.Fatalf("error: wanted %v; got %v\n", want, got)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestContextWithDeadline(t *testing.T) {
|
||
|
deadline := time.Now().Add(time.Second)
|
||
|
controlCtx, cancelControlCtx := context.WithDeadline(Background(), deadline.Add(time.Second))
|
||
|
defer cancelControlCtx()
|
||
|
ctx := WithDeadline(Background(), deadline)
|
||
|
defer ctx.Cancel()
|
||
|
if gotDeadline, ok := ctx.Deadline(); !ok {
|
||
|
t.Fatalf("error: deadline !ok\n")
|
||
|
} else if want, got := deadline, gotDeadline; want != got {
|
||
|
t.Fatalf("error: wanted deadline %s; got %s\n", want, got)
|
||
|
} else if err := ctx.Err(); err != nil {
|
||
|
t.Fatalf("error: wanted err == nil; got %v\n", err)
|
||
|
}
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
case <-controlCtx.Done():
|
||
|
t.Fatalf("error: control timeout reached - apicontext.WithDeadline failed to fire\n")
|
||
|
}
|
||
|
if want, got := context.DeadlineExceeded, ctx.Err(); !errors.Is(got, want) {
|
||
|
t.Fatalf("error: wanted err == %v; got %v\n", want, got)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestContextWithValue(t *testing.T) {
|
||
|
type ValueKey string
|
||
|
key, value := ValueKey("TheTestKey"), "TheTestValue"
|
||
|
ctx := WithValue(Background(), key, value)
|
||
|
// expect no error from cancel
|
||
|
defer func() {
|
||
|
if err := recover(); err != nil {
|
||
|
t.Fatalf("error: expected no panic; got %s\n", err)
|
||
|
}
|
||
|
}()
|
||
|
ctx.Cancel()
|
||
|
if want, got := value, ctx.Value(key).(string); want != got {
|
||
|
t.Fatalf("error: wanted %s; got %s\n", want, got)
|
||
|
} else if err := ctx.Err(); err != nil {
|
||
|
t.Fatalf("error: got error from context...\n")
|
||
|
}
|
||
|
}
|