carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package activator
2
3import (
4 "context"
5 "testing"
6 "time"
7)
8
9func TestCleanupBudgetSharedAfterCancellation(t *testing.T) {
10 t.Parallel()
11 parent, stop := context.WithCancel(t.Context())
12 ctx, finish := WithCleanupBudget(parent, 80*time.Millisecond)
13 defer finish()
14 stop()
15 first, cancel := CleanupContext(ctx, time.Second)
16 defer cancel()
17 deadline, ok := first.Deadline()
18 if !ok || time.Until(deadline) > 100*time.Millisecond {
19 t.Fatal("cleanup escaped shutdown budget")
20 }
21 nested, done := CleanupContext(first, time.Hour)
22 defer done()
23 if next, _ := nested.Deadline(); next.After(deadline) {
24 t.Fatal("nested cleanup extended deadline")
25 }
26 <-first.Done()
27 later, end := CleanupContext(ctx, time.Second)
28 defer end()
29 if later.Err() == nil {
30 t.Fatal("cleanup budget restarted")
31 }
32}
33
34func TestCleanupBudgetDoesNotLimitNormalOperation(t *testing.T) {
35 ctx, finish := WithCleanupBudget(t.Context(), time.Millisecond)
36 defer finish()
37 time.Sleep(3 * time.Millisecond)
38 cleanup, cancel := CleanupContext(ctx, time.Second)
39 defer cancel()
40 if cleanup.Err() != nil || ctx.Err() != nil {
41 t.Fatal("normal request exhausted shutdown budget")
42 }
43}
44
45func TestCleanupBudgetNestedOwnerUsesShutdownDeadline(t *testing.T) {
46 parent, stop := context.WithCancel(t.Context())
47 parent, finish := WithCleanupBudget(parent, 60*time.Millisecond)
48 defer finish()
49 request, cancel := context.WithCancel(parent)
50 defer cancel()
51 request, end := WithCleanupBudget(request, time.Second)
52 defer end()
53 stop()
54 owner, done := CleanupContext(parent, time.Second)
55 defer done()
56 handler, joined := CleanupContext(request, time.Second)
57 defer joined()
58 a, _ := owner.Deadline()
59 b, _ := handler.Deadline()
60 if !a.Equal(b) {
61 t.Fatalf("handler extended shared deadline: %v %v", a, b)
62 }
63}
64