trpc.group/trpc-go/trpc-go@v1.0.3/config/provider_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package config
    15  
    16  import (
    17  	"os"
    18  	"path/filepath"
    19  	"testing"
    20  
    21  	"github.com/fsnotify/fsnotify"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestProvider(t *testing.T) {
    26  	p := newFileProvider()
    27  
    28  	// read
    29  	buf, err := p.Read("../testdata/trpc_go.yaml")
    30  	assert.Nil(t, err)
    31  	assert.NotNil(t, buf)
    32  
    33  	// watch
    34  	cb := func(path string, data []byte) {}
    35  	p.Watch(cb)
    36  	os.WriteFile("../testdata/trpc_go.yaml", buf, 664)
    37  
    38  	p.disabledWatcher = true
    39  	p.watcher.Close()
    40  	_, err = p.Read("../testdata/trpc_go.yaml1")
    41  	assert.NotNil(t, err)
    42  }
    43  
    44  func TestIsModified(t *testing.T) {
    45  	filename := "../testdata/trpc_go.yaml3"
    46  	p := newFileProvider()
    47  	got, ok := p.isModified(fsnotify.Event{Name: filename})
    48  	assert.Zero(t, got)
    49  	assert.False(t, ok)
    50  
    51  	got, ok = p.isModified(fsnotify.Event{Op: fsnotify.Write, Name: filename})
    52  	assert.Zero(t, got)
    53  	assert.False(t, ok)
    54  
    55  	p.cache[filepath.Clean(filename)] = filename
    56  	got, ok = p.isModified(fsnotify.Event{Op: fsnotify.Write, Name: filename})
    57  	assert.Zero(t, got)
    58  	assert.False(t, ok)
    59  
    60  	os.WriteFile(filename, []byte("test"), 664)
    61  	defer os.Remove(filename)
    62  	got, ok = p.isModified(fsnotify.Event{Op: fsnotify.Write, Name: filename})
    63  	assert.NotZero(t, got)
    64  	assert.True(t, ok)
    65  
    66  	p.modTime[filename] = got + 10000
    67  	got, ok = p.isModified(fsnotify.Event{Op: fsnotify.Write, Name: filename})
    68  	assert.Zero(t, got)
    69  	assert.False(t, ok)
    70  }