|
| 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