github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/pkg/filenotify/poller_test.go (about)

     1  package filenotify
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"runtime"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/fsnotify/fsnotify"
    12  )
    13  
    14  func TestPollerAddRemove(t *testing.T) {
    15  	w := NewPollingWatcher()
    16  
    17  	if err := w.Add("no-such-file"); err == nil {
    18  		t.Fatal("should have gotten error when adding a non-existent file")
    19  	}
    20  	if err := w.Remove("no-such-file"); err == nil {
    21  		t.Fatal("should have gotten error when removing non-existent watch")
    22  	}
    23  
    24  	f, err := ioutil.TempFile("", "asdf")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	defer os.RemoveAll(f.Name())
    29  
    30  	if err := w.Add(f.Name()); err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	if err := w.Remove(f.Name()); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  }
    38  
    39  func TestPollerEvent(t *testing.T) {
    40  	if runtime.GOOS == "windows" {
    41  		t.Skip("No chmod on Windows")
    42  	}
    43  	w := NewPollingWatcher()
    44  
    45  	f, err := ioutil.TempFile("", "test-poller")
    46  	if err != nil {
    47  		t.Fatal("error creating temp file")
    48  	}
    49  	defer os.RemoveAll(f.Name())
    50  	f.Close()
    51  
    52  	if err := w.Add(f.Name()); err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	select {
    57  	case <-w.Events():
    58  		t.Fatal("got event before anything happened")
    59  	case <-w.Errors():
    60  		t.Fatal("got error before anything happened")
    61  	default:
    62  	}
    63  
    64  	if err := ioutil.WriteFile(f.Name(), []byte("hello"), 0644); err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	if err := assertEvent(w, fsnotify.Write); err != nil {
    68  		t.Fatal(err)
    69  	}
    70  
    71  	if err := os.Chmod(f.Name(), 600); err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	if err := assertEvent(w, fsnotify.Chmod); err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	if err := os.Remove(f.Name()); err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	if err := assertEvent(w, fsnotify.Remove); err != nil {
    82  		t.Fatal(err)
    83  	}
    84  }
    85  
    86  func TestPollerClose(t *testing.T) {
    87  	w := NewPollingWatcher()
    88  	if err := w.Close(); err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	// test double-close
    92  	if err := w.Close(); err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	f, err := ioutil.TempFile("", "asdf")
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	defer os.RemoveAll(f.Name())
   101  	if err := w.Add(f.Name()); err == nil {
   102  		t.Fatal("should have gotten error adding watch for closed watcher")
   103  	}
   104  }
   105  
   106  func assertEvent(w FileWatcher, eType fsnotify.Op) error {
   107  	var err error
   108  	select {
   109  	case e := <-w.Events():
   110  		if e.Op != eType {
   111  			err = fmt.Errorf("got wrong event type, expected %q: %v", eType, e.Op)
   112  		}
   113  	case e := <-w.Errors():
   114  		err = fmt.Errorf("got unexpected error waiting for events %v: %v", eType, e)
   115  	case <-time.After(watchWaitTime * 3):
   116  		err = fmt.Errorf("timeout waiting for event %v", eType)
   117  	}
   118  	return err
   119  }