github.com/wangyougui/gf/v2@v2.6.5/os/gfsnotify/gfsnotify_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gfsnotify_test 8 9 import ( 10 "testing" 11 "time" 12 13 "github.com/wangyougui/gf/v2/container/garray" 14 "github.com/wangyougui/gf/v2/container/gtype" 15 "github.com/wangyougui/gf/v2/os/gfile" 16 "github.com/wangyougui/gf/v2/os/gfsnotify" 17 "github.com/wangyougui/gf/v2/os/gtime" 18 "github.com/wangyougui/gf/v2/test/gtest" 19 "github.com/wangyougui/gf/v2/util/gconv" 20 ) 21 22 func TestWatcher_AddOnce(t *testing.T) { 23 gtest.C(t, func(t *gtest.T) { 24 value := gtype.New() 25 path := gfile.Temp(gconv.String(gtime.TimestampNano())) 26 err := gfile.PutContents(path, "init") 27 t.AssertNil(err) 28 defer gfile.Remove(path) 29 30 time.Sleep(100 * time.Millisecond) 31 callback1, err := gfsnotify.AddOnce("mywatch", path, func(event *gfsnotify.Event) { 32 value.Set(1) 33 }) 34 t.AssertNil(err) 35 callback2, err := gfsnotify.AddOnce("mywatch", path, func(event *gfsnotify.Event) { 36 value.Set(2) 37 }) 38 t.AssertNil(err) 39 t.Assert(callback2, nil) 40 41 err = gfile.PutContents(path, "1") 42 t.AssertNil(err) 43 44 time.Sleep(100 * time.Millisecond) 45 t.Assert(value, 1) 46 47 err = gfsnotify.RemoveCallback(callback1.Id) 48 t.AssertNil(err) 49 50 err = gfile.PutContents(path, "2") 51 t.AssertNil(err) 52 53 time.Sleep(100 * time.Millisecond) 54 t.Assert(value, 1) 55 }) 56 } 57 58 func TestWatcher_AddRemove(t *testing.T) { 59 gtest.C(t, func(t *gtest.T) { 60 path1 := gfile.Temp() + gfile.Separator + gconv.String(gtime.TimestampNano()) 61 path2 := gfile.Temp() + gfile.Separator + gconv.String(gtime.TimestampNano()) + "2" 62 gfile.PutContents(path1, "1") 63 defer func() { 64 gfile.Remove(path1) 65 gfile.Remove(path2) 66 }() 67 v := gtype.NewInt(1) 68 callback, err := gfsnotify.Add(path1, func(event *gfsnotify.Event) { 69 if event.IsWrite() { 70 v.Set(2) 71 return 72 } 73 if event.IsRename() { 74 v.Set(3) 75 gfsnotify.Exit() 76 return 77 } 78 }) 79 t.AssertNil(err) 80 t.AssertNE(callback, nil) 81 82 gfile.PutContents(path1, "2") 83 time.Sleep(100 * time.Millisecond) 84 t.Assert(v.Val(), 2) 85 86 gfile.Rename(path1, path2) 87 time.Sleep(100 * time.Millisecond) 88 t.Assert(v.Val(), 3) 89 }) 90 91 gtest.C(t, func(t *gtest.T) { 92 path1 := gfile.Temp() + gfile.Separator + gconv.String(gtime.TimestampNano()) 93 gfile.PutContents(path1, "1") 94 defer func() { 95 gfile.Remove(path1) 96 }() 97 v := gtype.NewInt(1) 98 callback, err := gfsnotify.Add(path1, func(event *gfsnotify.Event) { 99 if event.IsWrite() { 100 v.Set(2) 101 return 102 } 103 if event.IsRemove() { 104 v.Set(4) 105 return 106 } 107 }) 108 t.AssertNil(err) 109 t.AssertNE(callback, nil) 110 111 gfile.PutContents(path1, "2") 112 time.Sleep(100 * time.Millisecond) 113 t.Assert(v.Val(), 2) 114 115 gfile.Remove(path1) 116 time.Sleep(100 * time.Millisecond) 117 t.Assert(v.Val(), 4) 118 119 gfile.PutContents(path1, "1") 120 time.Sleep(100 * time.Millisecond) 121 t.Assert(v.Val(), 4) 122 }) 123 } 124 125 func TestWatcher_Callback1(t *testing.T) { 126 gtest.C(t, func(t *gtest.T) { 127 path1 := gfile.Temp(gtime.TimestampNanoStr()) 128 gfile.PutContents(path1, "1") 129 defer func() { 130 gfile.Remove(path1) 131 }() 132 v := gtype.NewInt(1) 133 callback, err := gfsnotify.Add(path1, func(event *gfsnotify.Event) { 134 if event.IsWrite() { 135 v.Set(2) 136 return 137 } 138 }) 139 t.AssertNil(err) 140 t.AssertNE(callback, nil) 141 142 gfile.PutContents(path1, "2") 143 time.Sleep(100 * time.Millisecond) 144 t.Assert(v.Val(), 2) 145 146 v.Set(3) 147 gfsnotify.RemoveCallback(callback.Id) 148 gfile.PutContents(path1, "3") 149 time.Sleep(100 * time.Millisecond) 150 t.Assert(v.Val(), 3) 151 }) 152 } 153 154 func TestWatcher_Callback2(t *testing.T) { 155 // multiple callbacks 156 gtest.C(t, func(t *gtest.T) { 157 path1 := gfile.Temp(gtime.TimestampNanoStr()) 158 t.Assert(gfile.PutContents(path1, "1"), nil) 159 defer func() { 160 gfile.Remove(path1) 161 }() 162 v1 := gtype.NewInt(1) 163 v2 := gtype.NewInt(1) 164 callback1, err1 := gfsnotify.Add(path1, func(event *gfsnotify.Event) { 165 if event.IsWrite() { 166 v1.Set(2) 167 return 168 } 169 }) 170 callback2, err2 := gfsnotify.Add(path1, func(event *gfsnotify.Event) { 171 if event.IsWrite() { 172 v2.Set(2) 173 return 174 } 175 }) 176 t.Assert(err1, nil) 177 t.Assert(err2, nil) 178 t.AssertNE(callback1, nil) 179 t.AssertNE(callback2, nil) 180 181 t.Assert(gfile.PutContents(path1, "2"), nil) 182 time.Sleep(100 * time.Millisecond) 183 t.Assert(v1.Val(), 2) 184 t.Assert(v2.Val(), 2) 185 186 v1.Set(3) 187 v2.Set(3) 188 gfsnotify.RemoveCallback(callback1.Id) 189 t.Assert(gfile.PutContents(path1, "3"), nil) 190 time.Sleep(100 * time.Millisecond) 191 t.Assert(v1.Val(), 3) 192 t.Assert(v2.Val(), 2) 193 }) 194 } 195 196 func TestWatcher_WatchFolderWithoutRecursively(t *testing.T) { 197 gtest.C(t, func(t *gtest.T) { 198 var ( 199 err error 200 array = garray.New(true) 201 dirPath = gfile.Temp(gtime.TimestampNanoStr()) 202 ) 203 err = gfile.Mkdir(dirPath) 204 t.AssertNil(err) 205 206 _, err = gfsnotify.Add(dirPath, func(event *gfsnotify.Event) { 207 // fmt.Println(event.String()) 208 array.Append(1) 209 }, false) 210 t.AssertNil(err) 211 time.Sleep(time.Millisecond * 100) 212 t.Assert(array.Len(), 0) 213 214 f, err := gfile.Create(gfile.Join(dirPath, "1")) 215 t.AssertNil(err) 216 t.AssertNil(f.Close()) 217 time.Sleep(time.Millisecond * 100) 218 t.Assert(array.Len(), 1) 219 }) 220 }