aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/howeyc/fsnotify/fsnotify_symlink_test.go
blob: 39061f84426ed0a5bf04ed8d40e4fc6e5012b87c (plain) (blame)
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
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build freebsd openbsd netbsd darwin linux

package fsnotify

import (
    "os"
    "path/filepath"
    "testing"
    "time"
)

func TestFsnotifyFakeSymlink(t *testing.T) {
    watcher := newWatcher(t)

    // Create directory to watch
    testDir := tempMkdir(t)
    defer os.RemoveAll(testDir)

    var errorsReceived counter
    // Receive errors on the error channel on a separate goroutine
    go func() {
        for errors := range watcher.Error {
            t.Logf("Received error: %s", errors)
            errorsReceived.increment()
        }
    }()

    // Count the CREATE events received
    var createEventsReceived, otherEventsReceived counter
    go func() {
        for ev := range watcher.Event {
            t.Logf("event received: %s", ev)
            if ev.IsCreate() {
                createEventsReceived.increment()
            } else {
                otherEventsReceived.increment()
            }
        }
    }()

    addWatch(t, watcher, testDir)

    if err := os.Symlink(filepath.Join(testDir, "zzz"), filepath.Join(testDir, "zzznew")); err != nil {
        t.Fatalf("Failed to create bogus symlink: %s", err)
    }
    t.Logf("Created bogus symlink")

    // We expect this event to be received almost immediately, but let's wait 500 ms to be sure
    time.Sleep(500 * time.Millisecond)

    // Should not be error, just no events for broken links (watching nothing)
    if errorsReceived.value() > 0 {
        t.Fatal("fsnotify errors have been received.")
    }
    if otherEventsReceived.value() > 0 {
        t.Fatal("fsnotify other events received on the broken link")
    }

    // Except for 1 create event (for the link itself)
    if createEventsReceived.value() == 0 {
        t.Fatal("fsnotify create events were not received after 500 ms")
    }
    if createEventsReceived.value() > 1 {
        t.Fatal("fsnotify more create events received than expected")
    }

    // Try closing the fsnotify instance
    t.Log("calling Close()")
    watcher.Close()
}