-
Notifications
You must be signed in to change notification settings - Fork 0
/
option_test.go
101 lines (87 loc) · 2.68 KB
/
option_test.go
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
// option_test contains tests for functional options that are ameable to unit
// testing. Options that require greater integration with the package as a whole
// are excercised throughout doppel_test and cache_operatoins_test.
package doppel
import (
"bytes"
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
)
// Go's log package recycles its buffer for each log entry, causing a data
// race if the buffer is accessed externally to the logger while logging is
// ongoing. testLogger preserves its buffer for analysis and protects it with
// a mutex.
type testLogger struct {
mu sync.Mutex
out *bytes.Buffer
}
func (tl *testLogger) Printf(msg string, data ...interface{}) {
tl.mu.Lock()
defer tl.mu.Unlock()
fmt.Fprintf(tl.out, msg+"\n", data...)
}
func (tl *testLogger) String() string {
tl.mu.Lock()
defer tl.mu.Unlock()
return tl.out.String()
}
func TestWithLogger(t *testing.T) {
t.Run("logs cache operations to the logger provded", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
l := &testLogger{out: &bytes.Buffer{}}
d, err := New(ctx, schematic, WithLogger(l))
if err != nil {
t.Fatal(err)
}
d.Get(context.Background(), "withBody1")
if gotLogs := l.String(); gotLogs == "" {
t.Error("failed to log operation, got empty string")
}
})
}
func TestWithGlobalTimeout(t *testing.T) {
t.Run("Get returns context.DeadlineExceeded when timeout expires", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
globalTimeout := 1 * time.Nanosecond
d, err := New(ctx, schematic, WithGlobalTimeout(globalTimeout))
if err != nil {
t.Fatal(err)
}
_, err = d.Get(context.Background(), "base")
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("want context.DeadlineExceeded, got: %v", err)
}
})
t.Run("Get times out after the shortest of global timeout and request timeout", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
globalTimeout := 1 * time.Millisecond
d, err := New(ctx, schematic, WithGlobalTimeout(globalTimeout))
if err != nil {
t.Fatal(err)
}
reqTimeout := 1 * time.Nanosecond
errStream := make(chan error)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), reqTimeout)
defer cancel()
_, err := d.Get(ctx, "withBody1")
errStream <- err
}()
select {
case err := <-errStream:
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("request timeout expired with error \"%v\"; want context.DeadlineExceeded",
err)
}
case <-time.After(globalTimeout):
t.Errorf("global timeout expired before Get returned; want request timeout to expire first")
}
})
}