go-micro.dev/v5@v5.12.0/config/source/file/watcher_test.go (about)

     1  package file_test
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  	"time"
    11  
    12  	"go-micro.dev/v5/config/source"
    13  	"go-micro.dev/v5/config/source/file"
    14  )
    15  
    16  // createTestFile a local helper to creates a temporary file with the given data
    17  func createTestFile(data []byte) (*os.File, func(), string, error) {
    18  	path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
    19  	fh, err := os.Create(path)
    20  	if err != nil {
    21  		return nil, func() {}, "", err
    22  	}
    23  
    24  	_, err = fh.Write(data)
    25  	if err != nil {
    26  		return nil, func() {}, "", err
    27  	}
    28  
    29  	return fh, func() {
    30  		fh.Close()
    31  		os.Remove(path)
    32  	}, path, err
    33  }
    34  
    35  func TestWatcher(t *testing.T) {
    36  	data := []byte(`{"foo": "bar"}`)
    37  	fh, cleanup, path, err := createTestFile(data)
    38  	if err != nil {
    39  		t.Error(err)
    40  	}
    41  	defer cleanup()
    42  
    43  	f := file.NewSource(file.WithPath(path))
    44  	if err != nil {
    45  		t.Error(err)
    46  	}
    47  
    48  	// create a watcher
    49  	w, err := f.Watch()
    50  	if err != nil {
    51  		t.Error(err)
    52  	}
    53  
    54  	newdata := []byte(`{"foo": "baz"}`)
    55  
    56  	go func() {
    57  		sc, err := w.Next()
    58  		if err != nil {
    59  			t.Error(err)
    60  			return
    61  		}
    62  
    63  		if !bytes.Equal(sc.Data, newdata) {
    64  			t.Error("expected data to be different")
    65  		}
    66  	}()
    67  
    68  	// rewrite to the file to trigger a change
    69  	_, err = fh.WriteAt(newdata, 0)
    70  	if err != nil {
    71  		t.Error(err)
    72  	}
    73  
    74  	// wait for the underlying watcher to detect changes
    75  	time.Sleep(time.Second)
    76  }
    77  
    78  func TestWatcherStop(t *testing.T) {
    79  	data := []byte(`{"foo": "bar"}`)
    80  	_, cleanup, path, err := createTestFile(data)
    81  	if err != nil {
    82  		t.Error(err)
    83  	}
    84  	defer cleanup()
    85  
    86  	src := file.NewSource(file.WithPath(path))
    87  	if err != nil {
    88  		t.Error(err)
    89  	}
    90  
    91  	// create a watcher
    92  	w, err := src.Watch()
    93  	if err != nil {
    94  		t.Error(err)
    95  	}
    96  
    97  	defer func() {
    98  		var err error
    99  		c := make(chan struct{})
   100  		defer close(c)
   101  
   102  		go func() {
   103  			_, err = w.Next()
   104  			c <- struct{}{}
   105  		}()
   106  
   107  		select {
   108  		case <-time.After(2 * time.Second):
   109  			err = errors.New("timeout waiting for Watcher.Next() to return")
   110  		case <-c:
   111  		}
   112  
   113  		if !errors.Is(err, source.ErrWatcherStopped) {
   114  			t.Error(err)
   115  		}
   116  	}()
   117  
   118  	// stop the watcher
   119  	w.Stop()
   120  }