| 1 |
package ctxext |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| 8 |
context "golang.org/x/net/context" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
func TestDeadline(t *testing.T) { |
| 13 |
if os.Getenv("TRAVIS") == "true" { |
| 14 |
t.Skip("timeouts don't work reliably on travis") |
| 15 |
} |
| 16 |
|
| 17 |
ctx, _ := context.WithTimeout(context.Background(), 5*time.Millisecond) |
| 18 |
|
| 19 |
select { |
| 20 |
case <-ctx.Done(): |
| 21 |
t.Fatal("ended too early") |
| 22 |
default: |
| 23 |
} |
| 24 |
|
| 25 |
<-time.After(6 * time.Millisecond) |
| 26 |
|
| 27 |
select { |
| 28 |
case <-ctx.Done(): |
| 29 |
default: |
| 30 |
t.Fatal("ended too late") |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func TestDeadlineFractionForever(t *testing.T) { |
| 35 |
|
| 36 |
ctx, _ := WithDeadlineFraction(context.Background(), 0.5) |
| 37 |
|
| 38 |
_, found := ctx.Deadline() |
| 39 |
if found { |
| 40 |
t.Fatal("should last forever") |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestDeadlineFractionHalf(t *testing.T) { |
| 45 |
if os.Getenv("TRAVIS") == "true" { |
| 46 |
t.Skip("timeouts don't work reliably on travis") |
| 47 |
} |
| 48 |
|
| 49 |
ctx1, _ := context.WithTimeout(context.Background(), 10*time.Millisecond) |
| 50 |
ctx2, _ := WithDeadlineFraction(ctx1, 0.5) |
| 51 |
|
| 52 |
select { |
| 53 |
case <-ctx1.Done(): |
| 54 |
t.Fatal("ctx1 ended too early") |
| 55 |
case <-ctx2.Done(): |
| 56 |
t.Fatal("ctx2 ended too early") |
| 57 |
default: |
| 58 |
} |
| 59 |
|
| 60 |
<-time.After(2 * time.Millisecond) |
| 61 |
|
| 62 |
select { |
| 63 |
case <-ctx1.Done(): |
| 64 |
t.Fatal("ctx1 ended too early") |
| 65 |
case <-ctx2.Done(): |
| 66 |
t.Fatal("ctx2 ended too early") |
| 67 |
default: |
| 68 |
} |
| 69 |
|
| 70 |
<-time.After(4 * time.Millisecond) |
| 71 |
|
| 72 |
select { |
| 73 |
case <-ctx1.Done(): |
| 74 |
t.Fatal("ctx1 ended too early") |
| 75 |
case <-ctx2.Done(): |
| 76 |
default: |
| 77 |
t.Fatal("ctx2 ended too late") |
| 78 |
} |
| 79 |
|
| 80 |
<-time.After(6 * time.Millisecond) |
| 81 |
|
| 82 |
select { |
| 83 |
case <-ctx1.Done(): |
| 84 |
default: |
| 85 |
t.Fatal("ctx1 ended too late") |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
func TestDeadlineFractionCancel(t *testing.T) { |
| 91 |
|
| 92 |
ctx1, cancel1 := context.WithTimeout(context.Background(), 10*time.Millisecond) |
| 93 |
ctx2, cancel2 := WithDeadlineFraction(ctx1, 0.5) |
| 94 |
|
| 95 |
select { |
| 96 |
case <-ctx1.Done(): |
| 97 |
t.Fatal("ctx1 ended too early") |
| 98 |
case <-ctx2.Done(): |
| 99 |
t.Fatal("ctx2 ended too early") |
| 100 |
default: |
| 101 |
} |
| 102 |
|
| 103 |
cancel2() |
| 104 |
|
| 105 |
select { |
| 106 |
case <-ctx1.Done(): |
| 107 |
t.Fatal("ctx1 should NOT be cancelled") |
| 108 |
case <-ctx2.Done(): |
| 109 |
default: |
| 110 |
t.Fatal("ctx2 should be cancelled") |
| 111 |
} |
| 112 |
|
| 113 |
cancel1() |
| 114 |
|
| 115 |
select { |
| 116 |
case <-ctx1.Done(): |
| 117 |
case <-ctx2.Done(): |
| 118 |
default: |
| 119 |
t.Fatal("ctx1 should be cancelled") |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
func TestDeadlineFractionObeysParent(t *testing.T) { |
| 125 |
|
| 126 |
ctx1, cancel1 := context.WithTimeout(context.Background(), 10*time.Millisecond) |
| 127 |
ctx2, _ := WithDeadlineFraction(ctx1, 0.5) |
| 128 |
|
| 129 |
select { |
| 130 |
case <-ctx1.Done(): |
| 131 |
t.Fatal("ctx1 ended too early") |
| 132 |
case <-ctx2.Done(): |
| 133 |
t.Fatal("ctx2 ended too early") |
| 134 |
default: |
| 135 |
} |
| 136 |
|
| 137 |
cancel1() |
| 138 |
|
| 139 |
select { |
| 140 |
case <-ctx2.Done(): |
| 141 |
default: |
| 142 |
t.Fatal("ctx2 should be cancelled") |
| 143 |
} |
| 144 |
|
| 145 |
} |