gf/os/gfsnotify/gfsnotify_z_unit_test.go

194 lines
4.5 KiB
Go
Raw Normal View History

2019-03-15 14:54:01 +08:00
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gfsnotify_test
import (
2019-07-22 15:31:35 +08:00
"testing"
"time"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/container/gtype"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/os/gfsnotify"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/util/gconv"
2019-03-15 14:54:01 +08:00
)
2019-11-01 15:31:26 +08:00
func TestWatcher_AddOnce(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-11-01 15:31:26 +08:00
value := gtype.New()
path := gfile.TempDir(gconv.String(gtime.TimestampNano()))
2019-11-01 15:31:26 +08:00
err := gfile.PutContents(path, "init")
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-11-01 15:31:26 +08:00
defer gfile.Remove(path)
time.Sleep(100 * time.Millisecond)
callback1, err := gfsnotify.AddOnce("mywatch", path, func(event *gfsnotify.Event) {
value.Set(1)
})
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-11-01 15:31:26 +08:00
callback2, err := gfsnotify.AddOnce("mywatch", path, func(event *gfsnotify.Event) {
value.Set(2)
})
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(callback2, nil)
2019-11-01 15:31:26 +08:00
err = gfile.PutContents(path, "1")
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-11-01 15:31:26 +08:00
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(value, 1)
2019-11-01 15:31:26 +08:00
err = gfsnotify.RemoveCallback(callback1.Id)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-11-01 15:31:26 +08:00
err = gfile.PutContents(path, "2")
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-11-01 15:31:26 +08:00
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(value, 1)
2019-11-01 15:31:26 +08:00
})
}
2019-03-15 14:54:01 +08:00
func TestWatcher_AddRemove(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
path1 := gfile.TempDir() + gfile.Separator + gconv.String(gtime.TimestampNano())
path2 := gfile.TempDir() + gfile.Separator + gconv.String(gtime.TimestampNano()) + "2"
2019-06-19 09:06:52 +08:00
gfile.PutContents(path1, "1")
defer func() {
gfile.Remove(path1)
gfile.Remove(path2)
}()
v := gtype.NewInt(1)
callback, err := gfsnotify.Add(path1, func(event *gfsnotify.Event) {
if event.IsWrite() {
v.Set(2)
return
}
if event.IsRename() {
v.Set(3)
gfsnotify.Exit()
return
}
})
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.AssertNE(callback, nil)
2019-03-15 14:54:01 +08:00
2019-06-19 09:06:52 +08:00
gfile.PutContents(path1, "2")
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(v.Val(), 2)
2019-03-15 14:54:01 +08:00
2019-06-19 09:06:52 +08:00
gfile.Rename(path1, path2)
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(v.Val(), 3)
2019-06-19 09:06:52 +08:00
})
2019-03-15 14:54:01 +08:00
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
path1 := gfile.TempDir() + gfile.Separator + gconv.String(gtime.TimestampNano())
2019-06-19 09:06:52 +08:00
gfile.PutContents(path1, "1")
defer func() {
gfile.Remove(path1)
}()
v := gtype.NewInt(1)
callback, err := gfsnotify.Add(path1, func(event *gfsnotify.Event) {
if event.IsWrite() {
v.Set(2)
return
}
if event.IsRemove() {
v.Set(4)
return
}
})
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.AssertNE(callback, nil)
2019-03-15 14:54:01 +08:00
2019-06-19 09:06:52 +08:00
gfile.PutContents(path1, "2")
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(v.Val(), 2)
2019-03-15 14:54:01 +08:00
2019-06-19 09:06:52 +08:00
gfile.Remove(path1)
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(v.Val(), 4)
2019-03-15 14:54:01 +08:00
2019-06-19 09:06:52 +08:00
gfile.PutContents(path1, "1")
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(v.Val(), 4)
2019-06-19 09:06:52 +08:00
})
2019-03-15 14:54:01 +08:00
}
func TestWatcher_Callback1(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
path1 := gfile.TempDir(gtime.TimestampNanoStr())
2019-06-19 09:06:52 +08:00
gfile.PutContents(path1, "1")
defer func() {
gfile.Remove(path1)
}()
v := gtype.NewInt(1)
callback, err := gfsnotify.Add(path1, func(event *gfsnotify.Event) {
if event.IsWrite() {
v.Set(2)
return
}
})
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.AssertNE(callback, nil)
2019-03-15 14:54:01 +08:00
2019-06-19 09:06:52 +08:00
gfile.PutContents(path1, "2")
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(v.Val(), 2)
2019-03-15 14:54:01 +08:00
2019-06-19 09:06:52 +08:00
v.Set(3)
gfsnotify.RemoveCallback(callback.Id)
gfile.PutContents(path1, "3")
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(v.Val(), 3)
2019-06-19 09:06:52 +08:00
})
}
func TestWatcher_Callback2(t *testing.T) {
2019-06-19 09:06:52 +08:00
// multiple callbacks
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
path1 := gfile.TempDir(gtime.TimestampNanoStr())
t.Assert(gfile.PutContents(path1, "1"), nil)
2019-06-19 09:06:52 +08:00
defer func() {
gfile.Remove(path1)
}()
v1 := gtype.NewInt(1)
v2 := gtype.NewInt(1)
callback1, err1 := gfsnotify.Add(path1, func(event *gfsnotify.Event) {
if event.IsWrite() {
v1.Set(2)
return
}
})
callback2, err2 := gfsnotify.Add(path1, func(event *gfsnotify.Event) {
if event.IsWrite() {
v2.Set(2)
return
}
})
2020-03-19 22:56:12 +08:00
t.Assert(err1, nil)
t.Assert(err2, nil)
t.AssertNE(callback1, nil)
t.AssertNE(callback2, nil)
2019-03-15 14:54:01 +08:00
t.Assert(gfile.PutContents(path1, "2"), nil)
2019-06-19 09:06:52 +08:00
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(v1.Val(), 2)
t.Assert(v2.Val(), 2)
2019-03-15 14:54:01 +08:00
2019-06-19 09:06:52 +08:00
v1.Set(3)
v2.Set(3)
gfsnotify.RemoveCallback(callback1.Id)
t.Assert(gfile.PutContents(path1, "3"), nil)
2019-06-19 09:06:52 +08:00
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(v1.Val(), 3)
t.Assert(v2.Val(), 2)
2019-06-19 09:06:52 +08:00
})
2019-03-15 14:54:01 +08:00
}