1
1
package fileutil
2
2
3
3
import (
4
+ "context"
5
+ "fmt"
4
6
"os"
5
7
"strconv"
6
8
"sync"
7
9
"testing"
8
10
"time"
11
+
12
+ "k8s.io/apimachinery/pkg/util/wait"
9
13
)
10
14
11
15
var origFileContent = `
@@ -17,22 +21,26 @@ efgwht2033
17
21
`
18
22
19
23
type testStruct struct {
20
- content string
24
+ callCount int
21
25
expectedContent string
22
26
mutex sync.Mutex
23
27
}
24
28
25
29
func (a * testStruct ) CallBackForFileLoad (dynamicContent []byte ) error {
26
30
a .mutex .Lock ()
27
- a .expectedContent = string (dynamicContent )
28
31
defer a .mutex .Unlock ()
32
+ a .callCount ++
33
+ if len (dynamicContent ) == 0 {
34
+ return fmt .Errorf ("file doesn't contain data" )
35
+ }
36
+ a .expectedContent = string (dynamicContent )
29
37
return nil
30
38
}
31
39
32
40
func (a * testStruct ) CallBackForFileDeletion () error {
33
41
a .mutex .Lock ()
34
- a .expectedContent = ""
35
42
defer a .mutex .Unlock ()
43
+ a .expectedContent = ""
36
44
return nil
37
45
}
38
46
@@ -60,13 +68,25 @@ func TestLoadDynamicFile(t *testing.T) {
60
68
}
61
69
stopCh := make (chan struct {})
62
70
testA := & testStruct {}
63
- StartLoadDynamicFile ("/tmp/util_test.txt" , testA , stopCh )
71
+ f , err := os .CreateTemp ("/tmp" , "testdata" )
72
+ if err != nil {
73
+ t .Errorf ("failed to create a local temp file %v" , err )
74
+ }
75
+ defer os .Remove (f .Name ())
76
+
77
+ StartLoadDynamicFile (f .Name (), testA , stopCh )
64
78
defer close (stopCh )
65
79
time .Sleep (2 * time .Second )
66
- os .WriteFile ("/tmp/util_test.txt" , []byte ("test" ), 0777 )
80
+ err = os .WriteFile (f .Name (), []byte ("test" ), 0777 )
81
+ if err != nil {
82
+ t .Errorf ("failed to update a temp file %s, err: %v" , f .Name (), err )
83
+ }
67
84
for {
68
85
time .Sleep (1 * time .Second )
69
86
testA .mutex .Lock ()
87
+ if testA .callCount != 3 {
88
+ t .Errorf ("load file should fail twice but call count is only %d" , testA .callCount )
89
+ }
70
90
if testA .expectedContent == "test" {
71
91
t .Log ("read to test" )
72
92
testA .mutex .Unlock ()
@@ -75,27 +95,36 @@ func TestLoadDynamicFile(t *testing.T) {
75
95
testA .mutex .Unlock ()
76
96
}
77
97
for _ , c := range cases {
78
- updateFile (testA , c .input , t )
79
- testA .mutex .Lock ()
98
+ updateFile (f .Name (), c .input , t )
99
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
100
+ //wait until the file reloaded is handled or context timesout
101
+ wait .Until (func () {
102
+ testA .mutex .Lock ()
103
+ defer testA .mutex .Unlock ()
104
+ // if the file is reloaded then cancel such that the wait.Until completes
105
+ if testA .expectedContent == c .want {
106
+ cancel ()
107
+ }
108
+ }, 100 * time .Millisecond , ctx .Done ())
109
+ cancel ()
110
+ //Validate the content
80
111
if testA .expectedContent != c .want {
81
112
t .Errorf (
82
113
"Unexpected result: TestLoadDynamicFile: got: %s, wanted %s" ,
83
114
testA .expectedContent ,
84
115
c .want ,
85
116
)
86
117
}
87
- testA .mutex .Unlock ()
88
118
}
119
+
89
120
}
90
121
91
- func updateFile (testA * testStruct , origFileContent string , t * testing.T ) {
92
- testA .content = origFileContent
122
+ func updateFile (fileName , origFileContent string , t * testing.T ) {
93
123
data := []byte (origFileContent )
94
- err := os .WriteFile ("/tmp/util_test.txt" , data , 0600 )
124
+ err := os .WriteFile (fileName , data , 0600 )
95
125
if err != nil {
96
- t .Errorf ("failed to create a local file /tmp/util_test.txt" )
126
+ t .Errorf ("failed to update a temp file %s, err: %v" , fileName , err )
97
127
}
98
- time .Sleep (1 * time .Second )
99
128
}
100
129
101
130
func TestDeleteDynamicFile (t * testing.T ) {
0 commit comments