-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenManager_test.go
114 lines (89 loc) · 2.61 KB
/
tokenManager_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
102
103
104
105
106
107
108
109
110
111
112
113
114
package tokenvault
import (
"context"
"fmt"
"testing"
"time"
)
// Mock TokenGeneratorFunc
func mockTokenGenerator() (interface{}, error) {
return "mock-token", nil
}
func TestNewTokenManager(t *testing.T) {
// Test case: Create a new TokenManager
duration := 10 * time.Second
tm := NewTokenManager("test", &duration, mockTokenGenerator)
if tm == nil {
t.Errorf("NewTokenManager returned nil")
return
}
if tm.name != "test" {
t.Errorf("Incorrect name. Expected: 'test', Got: '%s'", tm.name)
}
if tm.duration != duration {
t.Errorf("Incorrect duration. Expected: 10s, Got: %v", tm.duration)
}
if tm.generatorFunc == nil {
t.Error("generatorFunc is nil")
}
if tm.hasGeneratedFirstToken {
t.Error("hasGeneratedFirstToken should be false initially")
}
}
func TestGetToken(t *testing.T) {
// Test case: Get token before first token is generated
duration := 10 * time.Second
tm := NewTokenManager("test", &duration, mockTokenGenerator)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
doneCh := make(chan struct{})
go func() {
defer close(doneCh)
_ = tm.GetToken()
}()
select {
case <-doneCh:
// Test ran without any issues within the timeout
case <-ctx.Done():
if ctx.Err() == context.DeadlineExceeded {
t.Log("GetToken blocked as expected before first token generation")
} else {
t.Errorf("Unexpected error: %v", ctx.Err())
}
}
// Test case: Get token after first token is generated
tm.UpdateToken()
token := tm.GetToken()
if token != "mock-token" {
t.Errorf("Incorrect token. Expected: 'mock-token', Got: '%v'", token)
}
}
func TestUpdateToken(t *testing.T) {
// Test case: Update token successfully
duration := 1 * time.Second
tm := NewTokenManager("test", &duration, mockTokenGenerator)
go tm.RunTokenGenerator()
time.Sleep(5 * time.Second)
token := tm.GetToken()
fmt.Println("token", token)
if token != "mock-token" {
t.Errorf("Incorrect token. Expected: 'mock-token', Got: '%v'", token)
}
if !tm.hasGeneratedFirstToken {
t.Error("hasGeneratedFirstToken should be true after first token generation")
}
}
func TestRunTokenGenerator(t *testing.T) {
// Test case: Run token generator
duration := 500 * time.Millisecond
tm := NewTokenManager("test", &duration, mockTokenGenerator)
// Start the token generator in a separate goroutine
go tm.RunTokenGenerator()
// Wait for the first token generation
<-tm.blockFirstTime
// Check if the first token was generated successfully
token := tm.GetToken()
if token != "mock-token" {
t.Errorf("Incorrect token after first generation. Expected: 'mock-token', Got: '%v'", token)
}
}