-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifo_test.go
73 lines (55 loc) · 1.8 KB
/
lifo_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
package shutdown
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestLifoCloseContext(t *testing.T) {
lifo := &Lifo{}
first := &mockCloser{closeFunc: func() error { return nil }}
second := &mockCloser{closeFunc: func() error { return errors.New("second error") }}
third := &mockCloser{closeFunc: func() error { return nil }}
lifo.Append(first)
lifo.Append(second)
lifo.Append(third)
err := lifo.CloseContext(context.Background())
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "second error")
lifo = &Lifo{}
timeoutCloser := &mockCloser{closeFunc: func() error {
time.Sleep(2 * time.Second)
return nil
}}
lifo.Append(timeoutCloser)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
err = lifo.CloseContext(ctx)
assert.NotNil(t, err)
assert.True(t, errors.Is(err, context.DeadlineExceeded))
}
func TestLifoClose(t *testing.T) {
lifo := &Lifo{}
first := &mockCloser{closeFunc: func() error { return nil }}
second := &mockCloser{closeFunc: func() error { return errors.New("second error") }}
lifo.Append(first)
lifo.Append(second)
err := lifo.Close()
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "second error")
}
func TestLifo_WithContext(t *testing.T) {
l := &Lifo{}
// Embed the Lifo instance into a new context.
newCtx := l.WithContext(context.Background())
// Retrieve the Lifo instance (as a Closure) from the new context.
extractedClosure, ok := ClosureFromContext(newCtx)
if !ok {
t.Fatalf("Expected to find a closure in the context, but found none.")
}
// Type assert the extracted closure to *Lifo to verify it's the correct type.
if _, isLifo := extractedClosure.(*Lifo); !isLifo {
t.Fatalf("Expected the closure in context to be of type *Lifo, but it's not.")
}
}