-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_change_notification_test.go
121 lines (114 loc) · 3.53 KB
/
file_change_notification_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// -----------------------------------------------------------------------------
// ZR Library: Windows 32 API zr-win/[file_change_notification_test.go]
// (c) [email protected] License: MIT
// -----------------------------------------------------------------------------
package win
import (
"fmt"
"testing"
"github.com/balacode/zr"
)
// to test all items in file_change_notification.go use:
// go test --run Test_fchn_
//
// to generate a test coverage report for the whole module use:
// go test -coverprofile cover.out
// go tool cover -html=cover.out
// go test --run Test_fchn_FindFirstChangeNotification_
func Test_fchn_FindFirstChangeNotification_(t *testing.T) {
zr.TBegin(t)
// FindFirstChangeNotification(
// lpPathName string,
// bWatchSubtree BOOL,
// dwNotifyFilter DWORD,
// ) HANDLE
//
// create a subfolder called TEST
// write a timestamp to file A
// wait for one second
// write a timestamp to file B
//
const notify = FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_LAST_ACCESS |
FILE_NOTIFY_CHANGE_CREATION |
FILE_NOTIFY_CHANGE_SECURITY
//
// call with invalid folder:
// should return INVALID_HANDLE_VALUE i.e. -1 / 0xFFFFFFFFFFFFFFFF
got := HANDLE(FindFirstChangeNotification(`z:\yx`, TRUE, DWORD(notify)))
zr.TTrue(t, (got&INVALID_HANDLE_VALUE) == INVALID_HANDLE_VALUE)
//
// call with valid folder:
got = FindFirstChangeNotification(".", TRUE, DWORD(notify))
zr.TTrue(t, got != 0)
zr.TTrue(t, got < 65535)
} // Test_fchn_FindFirstChangeNotification_
// go test --run Test_fchn_FileChangeNotifications_
func Test_fchn_FileChangeNotifications_(t *testing.T) {
if true {
PL("Test_fchn_FileChangeNotifications_ IS UNIFINISHED")
return
}
path := `X:\TEST`
const notify = FILE_NOTIFY_CHANGE_CREATION |
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_SIZE |
0
tms := zr.Timestamp
//
// start watching the folder
handles := []HANDLE{
FindFirstChangeNotification(path, TRUE, DWORD(notify)),
}
// check that handle value is correct
switch handles[0] {
case INVALID_HANDLE_VALUE:
{
fmt.Println(tms(), "FindFirstChangeNotification() failed")
t.Fail()
return
}
case NULL:
{
fmt.Println(tms(), "FindFirstChangeNotification() returned NULL")
t.Fail()
return
}
default:
fmt.Println(tms(), "FindFirstChangeNotification() handle:",
handles[0])
}
for {
// wait for notification
fmt.Println(tms(), "Waiting.."+".")
status := WaitForMultipleObjects(1, &handles[0], TRUE, INFINITE)
fmt.Println(tms(), "Wait ended"+".")
switch status {
case WAIT_OBJECT_0:
fmt.Println(tms(), "DIRECTORY CHANGED!")
if FindNextChangeNotification(handles[0]) == FALSE {
fmt.Println(tms(), "FindNextChangeNotification() failed")
t.Fail()
return
}
case WAIT_OBJECT_0 + 1:
// notifications from other handle(s) which we don't have here
case WAIT_TIMEOUT:
// A timeout occurred, this would happen if some value other
// than INFINITE is used in the Wait call and no changes occur.
// In a single-threaded environment you might not want an
// INFINITE wait.
fmt.Println(tms(), "No changes during timeout period")
default:
fmt.Println(tms(), "Unhandled wait status", status)
t.Fail()
return
}
}
} // Test_fchn_FileChangeNotifications_
// end