Skip to content

Commit 3cfbe55

Browse files
committed
add module rjeczalik.notify
1 parent c2c6def commit 3cfbe55

File tree

5 files changed

+177
-1
lines changed

5 files changed

+177
-1
lines changed

modules/rjeczalik.notify/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module rjeczalik.notify
2+
3+
go 1.17
4+
5+
require github.com/rjeczalik/notify v0.9.2
6+
7+
require golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7 // indirect

modules/rjeczalik.notify/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8=
2+
github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM=
3+
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7 h1:bit1t3mgdR35yN0cX0G8orgLtOuyL9Wqxa1mccLB0ig=
4+
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

modules/rjeczalik.notify/main.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/rjeczalik/notify"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"time"
10+
)
11+
12+
var WaitFor = func(path string, e notify.Event, timeout time.Duration) bool {
13+
dir, file := filepath.Split(path)
14+
c := make(chan notify.EventInfo, 1)
15+
16+
if err := notify.Watch(dir, c, e); err != nil {
17+
log.Fatal(err)
18+
}
19+
// Clean up watchpoint associated with c. If Stop was not called upon
20+
// return the channel would be leaked as notify holds the only reference
21+
// to it and does not release it on its own.
22+
defer notify.Stop(c)
23+
24+
t := time.After(timeout)
25+
26+
for {
27+
select {
28+
case ei := <-c:
29+
fmt.Println(filepath.Base(ei.Path()), file, ei.Event())
30+
if filepath.Base(ei.Path()) == file {
31+
return true
32+
}
33+
case <-t:
34+
return false
35+
}
36+
}
37+
}
38+
39+
var filename = "test.txt"
40+
41+
func main() {
42+
go func() {
43+
os.Remove(filename)
44+
time.Sleep(time.Second * 3)
45+
os.Create(filename)
46+
time.Sleep(time.Second * 3)
47+
if err := os.Remove(filename); err != nil {
48+
fmt.Printf("remove: %s", err)
49+
}
50+
}()
51+
52+
go func() {
53+
if WaitFor(filename, notify.Create, time.Second*10) {
54+
log.Println(filename, "locked")
55+
}
56+
}()
57+
58+
go func() {
59+
if WaitFor(filename, notify.All, time.Second*10) {
60+
log.Println(filename, "locked2")
61+
}
62+
}()
63+
64+
if WaitFor(filename, notify.Remove, time.Second*15) {
65+
log.Println(filename, "unlocked")
66+
}
67+
68+
fmt.Println("over")
69+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main_test
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"path/filepath"
7+
"testing"
8+
"time"
9+
10+
"github.com/rjeczalik/notify"
11+
)
12+
13+
func TestBasic(t *testing.T) {
14+
// Make the channel buffered to ensure no event is dropped. Notify will drop
15+
// an event if the receiver is not able to keep up the sending pace.
16+
c := make(chan notify.EventInfo, 1)
17+
18+
// Set up a watchpoint listening on events within current working directory.
19+
// Dispatch each create and remove events separately to c.
20+
if err := notify.Watch(".", c, notify.Create, notify.Remove); err != nil {
21+
log.Fatal(err)
22+
}
23+
defer notify.Stop(c)
24+
25+
// Block until an event is received.
26+
ei := <-c
27+
log.Println("Got event:", ei)
28+
}
29+
30+
func TestRecursive(t *testing.T) {
31+
// Make the channel buffered to ensure no event is dropped. Notify will drop
32+
// an event if the receiver is not able to keep up the sending pace.
33+
c := make(chan notify.EventInfo, 1)
34+
35+
// Set up a watchpoint listening for events within a directory tree rooted
36+
// at current working directory. Dispatch remove events to c.
37+
if err := notify.Watch("./...", c, notify.Remove); err != nil {
38+
log.Fatal(err)
39+
}
40+
defer notify.Stop(c)
41+
42+
// Block until an event is received.
43+
ei := <-c
44+
log.Println("Got event:", ei)
45+
}
46+
47+
func TestWatch(t *testing.T) {
48+
c := make(chan notify.EventInfo, 3)
49+
if err := notify.Watch(".", c, notify.All); err != nil {
50+
log.Fatal(err)
51+
}
52+
defer notify.Stop(c)
53+
54+
for {
55+
select {
56+
case e := <-c:
57+
fmt.Println(e)
58+
}
59+
}
60+
}
61+
62+
func TestName(t *testing.T) {
63+
waitfor := func(path string, e notify.Event, timeout time.Duration) bool {
64+
dir, file := filepath.Split(path)
65+
c := make(chan notify.EventInfo, 1)
66+
67+
if err := notify.Watch(dir, c, e); err != nil {
68+
log.Fatal(err)
69+
}
70+
// Clean up watchpoint associated with c. If Stop was not called upon
71+
// return the channel would be leaked as notify holds the only reference
72+
// to it and does not release it on its own.
73+
defer notify.Stop(c)
74+
75+
t := time.After(timeout)
76+
77+
for {
78+
select {
79+
case ei := <-c:
80+
if filepath.Base(ei.Path()) == file {
81+
return true
82+
}
83+
case <-t:
84+
return false
85+
}
86+
}
87+
}
88+
89+
if waitfor("index.lock", notify.Create, 5*time.Second) {
90+
log.Println("The git repository was locked")
91+
}
92+
93+
if waitfor("index.lock", notify.Remove, 5*time.Second) {
94+
log.Println("The git repository was unlocked")
95+
}
96+
}

0 commit comments

Comments
 (0)