-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
76 lines (62 loc) · 1.84 KB
/
example_test.go
File metadata and controls
76 lines (62 loc) · 1.84 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package lifecycle_test
import (
"context"
"fmt"
"time"
"github.com/aretw0/lifecycle"
)
// ExampleNewSignalContext demonstrates how to use the Dual Signal context.
// Note: This example is illustrative; in a real run, it waits for SIGINT/SIGTERM.
func ExampleNewSignalContext() {
// Create a context that listens for signals.
ctx := lifecycle.NewSignalContext(context.Background())
// For checking output deterministically in this example, we cancel manually
// after a short delay, allowing "work" to happen first.
go func() {
time.Sleep(50 * time.Millisecond)
ctx.Cancel()
}()
// Simulate work
select {
case <-ctx.Done():
fmt.Println("Context cancelled too early")
case <-time.After(10 * time.Millisecond):
fmt.Println("Doing work...")
}
// Output:
// Doing work...
}
// ExampleOpenTerminal demonstrates how to open the terminal safely.
func ExampleOpenTerminal() {
// OpenTerminal handles OS-specific logic (like CONIN$ on Windows)
reader, err := lifecycle.OpenTerminal()
if err != nil {
fmt.Printf("Error opening terminal: %v\n", err)
return
}
defer reader.Close()
fmt.Println("Terminal opened successfully")
// Wrap with InterruptibleReader to respect context cancellation
// r := lifecycle.NewInterruptibleReader(reader, ctx.Done())
// Output:
// Terminal opened successfully
}
// ExampleBlockWithTimeout demonstrates how to enforce a deadline on shutdown cleanup.
func ExampleBlockWithTimeout() {
done := make(chan struct{})
// Simulate a cleanup task
go func() {
defer close(done)
// Simulate fast cleanup
time.Sleep(10 * time.Millisecond)
}()
// Wait for cleanup, but give up after 1 second
err := lifecycle.BlockWithTimeout(done, 1*time.Second)
if err != nil {
fmt.Println("Cleanup timed out!")
} else {
fmt.Println("Cleanup finished successfully")
}
// Output:
// Cleanup finished successfully
}