-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
194 lines (156 loc) · 4.8 KB
/
example_test.go
File metadata and controls
194 lines (156 loc) · 4.8 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package timecapsule
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// Example_basic demonstrates basic usage of the time capsule
func Example_basic() {
// Create a new time capsule
capsule := New[string]()
// Store a value that unlocks in 1 second
unlockTime := time.Now().Add(1 * time.Second)
err := capsule.Store(context.Background(), "greeting", "Hello, World!", unlockTime)
if err != nil {
panic(err)
}
// Try to open immediately - should be locked
if _, openErr := capsule.Open(context.Background(), "greeting"); openErr != nil {
fmt.Println("Capsule is locked:", openErr)
}
// Wait for unlock
time.Sleep(2 * time.Second)
// Now open the capsule
value, err := capsule.Open(context.Background(), "greeting")
if err != nil {
panic(err)
}
fmt.Println("Unlocked value:", value)
// Output:
// Capsule is locked: capsule is still locked
// Unlocked value: Hello, World!
}
// Example_struct demonstrates using structs with the time capsule
func Example_struct() {
type Promo struct {
Code string `json:"code"`
Discount int `json:"discount"`
Valid bool `json:"valid"`
}
// Create a time capsule for Promo structs
capsule := New[Promo]()
// Store a promotional code that unlocks tomorrow
tomorrow := time.Now().Add(24 * time.Hour)
promo := Promo{
Code: "HOLIDAY50",
Discount: 50,
Valid: true,
}
err := capsule.Store(context.Background(), "holiday-sale", promo, tomorrow)
if err != nil {
panic(err)
}
// Check metadata without opening
metadata, err := capsule.Peek(context.Background(), "holiday-sale")
if err != nil {
panic(err)
}
fmt.Printf("Promo unlocks in: %v\n", time.Until(metadata.UnlockTime).Round(time.Hour))
fmt.Printf("Is locked: %v\n", metadata.IsLocked)
// Output:
// Promo unlocks in: 24h0m0s
// Is locked: true
}
// Example_waitForUnlock demonstrates waiting for a capsule to unlock
func Example_waitForUnlock() {
capsule := New[int]()
// Store a value that unlocks in 100ms
unlockTime := time.Now().Add(100 * time.Millisecond)
err := capsule.Store(context.Background(), "count", 42, unlockTime)
if err != nil {
panic(err)
}
// Wait for unlock with timeout
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
value, err := capsule.WaitForUnlock(ctx, "count")
if err != nil {
panic(err)
}
fmt.Printf("Waited for unlock, got value: %d\n", value)
// Output:
// Waited for unlock, got value: 42
}
// Example_context demonstrates using context for cancellation
func Example_context() {
capsule := New[string]()
// Store a value that unlocks in 5 seconds
unlockTime := time.Now().Add(5 * time.Second)
err := capsule.Store(context.Background(), "slow", "This takes time", unlockTime)
if err != nil {
panic(err)
}
// Create a context that cancels after 1 second
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
// Try to wait for unlock, but context will cancel first
_, err = capsule.WaitForUnlock(ctx, "slow")
if err != nil {
fmt.Println("Context canceled:", err)
}
// Output:
// Context canceled: context deadline exceeded
}
// Example_management demonstrates capsule management operations
func Example_management() {
capsule := New[float64]()
// Store multiple values
now := time.Now()
var err error
err = capsule.Store(context.Background(), "price1", 19.99, now.Add(1*time.Hour))
if err != nil {
panic(err)
}
err = capsule.Store(context.Background(), "price2", 29.99, now.Add(2*time.Hour))
if err != nil {
panic(err)
}
// Check if capsules exist
fmt.Printf("price1 exists: %v\n", capsule.Exists(context.Background(), "price1"))
fmt.Printf("price3 exists: %v\n", capsule.Exists(context.Background(), "price3"))
// Delete a capsule
err = capsule.Delete(context.Background(), "price1")
if err != nil {
panic(err)
}
fmt.Printf("After delete, price1 exists: %v\n", capsule.Exists(context.Background(), "price1"))
// Output:
// price1 exists: true
// price3 exists: false
// After delete, price1 exists: false
}
// TestDelayExample demonstrates delaying a capsule's unlock time
func TestDelayExample(t *testing.T) {
capsule := New[string]()
ctx := context.Background()
// Store a value that unlocks in 1 hour
unlockTime := time.Now().Add(1 * time.Hour)
err := capsule.Store(ctx, "secret", "confidential", unlockTime)
if err != nil {
panic(err)
}
// Check initial unlock time
metadata, _ := capsule.Peek(ctx, "secret")
fmt.Printf("Original unlock time: %v\n", metadata.UnlockTime.Format("15:04"))
// Delay by 2 more hours
err = capsule.Delay(ctx, "secret", 2*time.Hour)
if err != nil {
panic(err)
}
// Check new unlock time
metadata, _ = capsule.Peek(ctx, "secret")
fmt.Printf("New unlock time: %v\n", metadata.UnlockTime.Format("15:04"))
assert.Greater(t, metadata.UnlockTime, unlockTime)
}