-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground_test.go
More file actions
50 lines (38 loc) · 1.18 KB
/
Copy pathbackground_test.go
File metadata and controls
50 lines (38 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package botapi
import (
"context"
"testing"
)
func TestBackgroundContext(t *testing.T) {
b := newTestBot(t)
// Before Run, Background is already canceled so background sends fail fast.
if err := b.Background().Err(); err == nil {
t.Fatal("Background before Run should be canceled")
}
// While running, it returns the live run context.
ctx, cancel := context.WithCancel(context.Background())
b.setRunCtx(ctx)
if got := b.Background(); got != ctx {
t.Fatalf("Background should return the run context, got %v", got)
}
if b.Background().Err() != nil {
t.Fatal("live run context should not be canceled")
}
// Canceling the run context (bot stopping) cancels background work.
cancel()
if b.Background().Err() == nil {
t.Fatal("canceled run context should propagate")
}
// After Run returns, runCtx is cleared back to a canceled context.
b.setRunCtx(nil)
if b.Background().Err() == nil {
t.Fatal("Background after stop should be canceled")
}
}
func TestContextBackgroundDelegates(t *testing.T) {
b := newTestBot(t)
c := &Context{Bot: b, Update: &Update{}}
if c.Background() != b.Background() {
t.Fatal("Context.Background should delegate to Bot.Background")
}
}