-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_example_test.go
More file actions
54 lines (47 loc) · 1.54 KB
/
Copy pathcontext_example_test.go
File metadata and controls
54 lines (47 loc) · 1.54 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
51
52
53
54
package core_test
import (
. "dappco.re/go"
)
// ExampleBackground creates a background context through `Background` for request lifetime
// control. Cancellation and timeout lifetimes are created through the core context
// surface.
func ExampleBackground() {
ctx := Background()
Println(ctx.Err() == nil)
// Output: true
}
// ExampleWithTimeout creates a timeout context through `WithTimeout` for request lifetime
// control. Cancellation and timeout lifetimes are created through the core context
// surface.
func ExampleWithTimeout() {
ctx, cancel := WithTimeout(Background(), 50*Millisecond)
defer cancel()
Println(ctx.Err() == nil)
// Output: true
}
// ExampleWithCancel creates a cancellable context through `WithCancel` for request
// lifetime control. Cancellation and timeout lifetimes are created through the core
// context surface.
func ExampleWithCancel() {
ctx, cancel := WithCancel(Background())
cancel()
Println(ctx.Err() != nil)
// Output: true
}
// ExampleTODO returns a non-nil placeholder context through `TODO`.
func ExampleTODO() {
Println(TODO() != nil)
// Output: true
}
// ExampleWithValue carries a request-scoped value through `WithValue`.
func ExampleWithValue() {
ctx := WithValue(Background(), "agent", "codex")
Println(ctx.Value("agent"))
// Output: codex
}
// ExampleWithDeadline derives a context that cancels at a deadline through `WithDeadline`.
func ExampleWithDeadline() {
ctx, cancel := WithDeadline(Background(), Now().Add(Hour))
defer cancel()
_ = ctx // cancels automatically once the deadline passes
}