11package fileutil
22
33import (
4+ "context"
5+ "fmt"
46 "os"
57 "strconv"
68 "sync"
79 "testing"
810 "time"
11+
12+ "k8s.io/apimachinery/pkg/util/wait"
913)
1014
1115var origFileContent = `
@@ -17,22 +21,26 @@ efgwht2033
1721`
1822
1923type testStruct struct {
20- content string
24+ callCount int
2125 expectedContent string
2226 mutex sync.Mutex
2327}
2428
2529func (a * testStruct ) CallBackForFileLoad (dynamicContent []byte ) error {
2630 a .mutex .Lock ()
27- a .expectedContent = string (dynamicContent )
2831 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 )
2937 return nil
3038}
3139
3240func (a * testStruct ) CallBackForFileDeletion () error {
3341 a .mutex .Lock ()
34- a .expectedContent = ""
3542 defer a .mutex .Unlock ()
43+ a .expectedContent = ""
3644 return nil
3745}
3846
@@ -60,13 +68,25 @@ func TestLoadDynamicFile(t *testing.T) {
6068 }
6169 stopCh := make (chan struct {})
6270 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 )
6478 defer close (stopCh )
6579 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+ }
6784 for {
6885 time .Sleep (1 * time .Second )
6986 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+ }
7090 if testA .expectedContent == "test" {
7191 t .Log ("read to test" )
7292 testA .mutex .Unlock ()
@@ -75,27 +95,36 @@ func TestLoadDynamicFile(t *testing.T) {
7595 testA .mutex .Unlock ()
7696 }
7797 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
80111 if testA .expectedContent != c .want {
81112 t .Errorf (
82113 "Unexpected result: TestLoadDynamicFile: got: %s, wanted %s" ,
83114 testA .expectedContent ,
84115 c .want ,
85116 )
86117 }
87- testA .mutex .Unlock ()
88118 }
119+
89120}
90121
91- func updateFile (testA * testStruct , origFileContent string , t * testing.T ) {
92- testA .content = origFileContent
122+ func updateFile (fileName , origFileContent string , t * testing.T ) {
93123 data := []byte (origFileContent )
94- err := os .WriteFile ("/tmp/util_test.txt" , data , 0600 )
124+ err := os .WriteFile (fileName , data , 0600 )
95125 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 )
97127 }
98- time .Sleep (1 * time .Second )
99128}
100129
101130func TestDeleteDynamicFile (t * testing.T ) {
0 commit comments