...
1
16
17 package rest
18
19 import (
20 "context"
21 "testing"
22 "time"
23 )
24
25 func TestContextFromChannelAndMaxWaitDurationWithChannelClosed(t *testing.T) {
26 stopCh := make(chan struct{})
27 ctx, cancel := contextFromChannelAndMaxWaitDuration(stopCh, time.Hour)
28 defer cancel()
29
30 select {
31 case <-ctx.Done():
32 t.Fatalf("Expected the derived context to be not cancelled, but got: %v", ctx.Err())
33 default:
34 }
35
36 close(stopCh)
37
38 <-ctx.Done()
39 if ctx.Err() != context.Canceled {
40 t.Errorf("Expected the context to be canceled with: %v, but got: %v", context.Canceled, ctx.Err())
41 }
42 }
43
44 func TestContextFromChannelAndMaxWaitDurationWithMaxWaitElapsed(t *testing.T) {
45 stopCh := make(chan struct{})
46 ctx, cancel := contextFromChannelAndMaxWaitDuration(stopCh, 100*time.Millisecond)
47 defer cancel()
48
49 <-ctx.Done()
50
51 if ctx.Err() != context.Canceled {
52 t.Errorf("Expected the context to be canceled with: %v, but got: %v", context.Canceled, ctx.Err())
53 }
54 }
55
View as plain text