-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobust_test.go
More file actions
78 lines (70 loc) · 2.28 KB
/
Copy pathrobust_test.go
File metadata and controls
78 lines (70 loc) · 2.28 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
package env_test
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/zxdev/env"
)
// TestExpireTTLFailsafe guards the mass-deletion footgun: a ttl argument of an
// unsupported type (time.Duration is the natural mistake), or a zero, must fall
// back to the 24h default rather than registering TTL=0 and deleting everything.
func TestExpireTTLFailsafe(t *testing.T) {
dir := t.TempDir()
keep := filepath.Join(dir, "recent.txt")
if err := os.WriteFile(keep, []byte("x"), 0644); err != nil {
t.Fatal(err)
}
// age the file a couple seconds so a TTL=0 bug would expire it immediately
old := time.Now().Add(-2 * time.Second)
if err := os.Chtimes(keep, old, old); err != nil {
t.Fatal(err)
}
// time.Duration is unsupported by the type switch -> must default to 24h
var ex env.Expire
ex.Silent().Add(time.Hour, dir).Expire()
if _, err := os.Stat(keep); err != nil {
t.Fatalf("recent file deleted: unsupported ttl type fell through to TTL=0 (%v)", err)
}
}
// TestPersistLoadNoPanic guards the nil-deref: Load on a missing file with a ttl
// must report "gone" and not panic.
func TestPersistLoadNoPanic(t *testing.T) {
p := env.Persist(filepath.Join(t.TempDir(), "missing"))
ttl := time.Hour
var dst map[string]time.Time
if !p.Load(&dst, &ttl) { // missing file -> treated as gone -> true
t.Fatal("Load of missing file should return true (nothing to resume)")
}
}
// TestPersistRoundTrip confirms Save/Load still works after the encode-error fix.
func TestPersistRoundTrip(t *testing.T) {
p := env.Persist(filepath.Join(t.TempDir(), "state"))
src := map[string]int{"a": 1, "b": 2}
if !p.Save(src) {
t.Fatal("Save failed")
}
var dst map[string]int
if !p.Load(&dst, nil) {
t.Fatal("Load failed")
}
if dst["a"] != 1 || dst["b"] != 2 {
t.Fatalf("round trip mismatch: %v", dst)
}
}
// TestGraceInitBadSignature guards the deadlock: an unsupported Init func
// signature must still release its init slot so Wait() returns.
func TestGraceInitBadSignature(t *testing.T) {
done := make(chan struct{})
go func() {
grace := env.NewGraceful().Silent()
grace.Init(func() error { return nil }) // unsupported signature
grace.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("Init with unsupported signature deadlocked Wait()")
}
}