github.com/zhongdalu/gf@v1.0.0/g/os/gfsnotify/gfsnotify_z_unit_test.go (about) 1 // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 // go test *.go -bench=".*" -benchmem 8 9 package gfsnotify_test 10 11 import ( 12 "testing" 13 "time" 14 15 "github.com/zhongdalu/gf/g/container/gtype" 16 "github.com/zhongdalu/gf/g/os/gfile" 17 "github.com/zhongdalu/gf/g/os/gfsnotify" 18 "github.com/zhongdalu/gf/g/os/gtime" 19 "github.com/zhongdalu/gf/g/test/gtest" 20 "github.com/zhongdalu/gf/g/util/gconv" 21 ) 22 23 func TestWatcher_AddRemove(t *testing.T) { 24 gtest.Case(t, func() { 25 path1 := gfile.TempDir() + gfile.Separator + gconv.String(gtime.Nanosecond()) 26 path2 := gfile.TempDir() + gfile.Separator + gconv.String(gtime.Nanosecond()) + "2" 27 gfile.PutContents(path1, "1") 28 defer func() { 29 gfile.Remove(path1) 30 gfile.Remove(path2) 31 }() 32 v := gtype.NewInt(1) 33 callback, err := gfsnotify.Add(path1, func(event *gfsnotify.Event) { 34 if event.IsWrite() { 35 v.Set(2) 36 return 37 } 38 if event.IsRename() { 39 v.Set(3) 40 gfsnotify.Exit() 41 return 42 } 43 }) 44 gtest.Assert(err, nil) 45 gtest.AssertNE(callback, nil) 46 47 gfile.PutContents(path1, "2") 48 time.Sleep(100 * time.Millisecond) 49 gtest.Assert(v.Val(), 2) 50 51 gfile.Rename(path1, path2) 52 time.Sleep(100 * time.Millisecond) 53 gtest.Assert(v.Val(), 3) 54 }) 55 56 gtest.Case(t, func() { 57 path1 := gfile.TempDir() + gfile.Separator + gconv.String(gtime.Nanosecond()) 58 gfile.PutContents(path1, "1") 59 defer func() { 60 gfile.Remove(path1) 61 }() 62 v := gtype.NewInt(1) 63 callback, err := gfsnotify.Add(path1, func(event *gfsnotify.Event) { 64 if event.IsWrite() { 65 v.Set(2) 66 return 67 } 68 if event.IsRemove() { 69 v.Set(4) 70 return 71 } 72 }) 73 gtest.Assert(err, nil) 74 gtest.AssertNE(callback, nil) 75 76 gfile.PutContents(path1, "2") 77 time.Sleep(100 * time.Millisecond) 78 gtest.Assert(v.Val(), 2) 79 80 gfile.Remove(path1) 81 time.Sleep(100 * time.Millisecond) 82 gtest.Assert(v.Val(), 4) 83 84 gfile.PutContents(path1, "1") 85 time.Sleep(100 * time.Millisecond) 86 gtest.Assert(v.Val(), 4) 87 }) 88 } 89 90 func TestWatcher_Callback(t *testing.T) { 91 gtest.Case(t, func() { 92 path1 := gfile.TempDir() + gfile.Separator + gconv.String(gtime.Nanosecond()) 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 }) 104 gtest.Assert(err, nil) 105 gtest.AssertNE(callback, nil) 106 107 gfile.PutContents(path1, "2") 108 time.Sleep(100 * time.Millisecond) 109 gtest.Assert(v.Val(), 2) 110 111 v.Set(3) 112 gfsnotify.RemoveCallback(callback.Id) 113 gfile.PutContents(path1, "3") 114 time.Sleep(100 * time.Millisecond) 115 gtest.Assert(v.Val(), 3) 116 }) 117 // multiple callbacks 118 gtest.Case(t, func() { 119 path1 := gfile.TempDir() + gfile.Separator + gconv.String(gtime.Nanosecond()) 120 gfile.PutContents(path1, "1") 121 defer func() { 122 gfile.Remove(path1) 123 }() 124 v1 := gtype.NewInt(1) 125 v2 := gtype.NewInt(1) 126 callback1, err1 := gfsnotify.Add(path1, func(event *gfsnotify.Event) { 127 if event.IsWrite() { 128 v1.Set(2) 129 return 130 } 131 }) 132 callback2, err2 := gfsnotify.Add(path1, func(event *gfsnotify.Event) { 133 if event.IsWrite() { 134 v2.Set(2) 135 return 136 } 137 }) 138 gtest.Assert(err1, nil) 139 gtest.Assert(err2, nil) 140 gtest.AssertNE(callback1, nil) 141 gtest.AssertNE(callback2, nil) 142 143 gfile.PutContents(path1, "2") 144 time.Sleep(100 * time.Millisecond) 145 gtest.Assert(v1.Val(), 2) 146 gtest.Assert(v2.Val(), 2) 147 148 v1.Set(3) 149 v2.Set(3) 150 gfsnotify.RemoveCallback(callback1.Id) 151 gfile.PutContents(path1, "3") 152 time.Sleep(100 * time.Millisecond) 153 gtest.Assert(v1.Val(), 3) 154 gtest.Assert(v2.Val(), 2) 155 }) 156 }