github.com/micro/go-micro/v2@v2.9.1/config/default_test.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/micro/go-micro/v2/config/source"
    13  	"github.com/micro/go-micro/v2/config/source/env"
    14  	"github.com/micro/go-micro/v2/config/source/file"
    15  	"github.com/micro/go-micro/v2/config/source/memory"
    16  )
    17  
    18  func createFileForIssue18(t *testing.T, content string) *os.File {
    19  	data := []byte(content)
    20  	path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
    21  	fh, err := os.Create(path)
    22  	if err != nil {
    23  		t.Error(err)
    24  	}
    25  	_, err = fh.Write(data)
    26  	if err != nil {
    27  		t.Error(err)
    28  	}
    29  
    30  	return fh
    31  }
    32  
    33  func createFileForTest(t *testing.T) *os.File {
    34  	data := []byte(`{"foo": "bar"}`)
    35  	path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
    36  	fh, err := os.Create(path)
    37  	if err != nil {
    38  		t.Error(err)
    39  	}
    40  	_, err = fh.Write(data)
    41  	if err != nil {
    42  		t.Error(err)
    43  	}
    44  
    45  	return fh
    46  }
    47  
    48  func TestConfigLoadWithGoodFile(t *testing.T) {
    49  	fh := createFileForTest(t)
    50  	path := fh.Name()
    51  	defer func() {
    52  		fh.Close()
    53  		os.Remove(path)
    54  	}()
    55  
    56  	// Create new config
    57  	conf, err := NewConfig()
    58  	if err != nil {
    59  		t.Fatalf("Expected no error but got %v", err)
    60  	}
    61  	// Load file source
    62  	if err := conf.Load(file.NewSource(
    63  		file.WithPath(path),
    64  	)); err != nil {
    65  		t.Fatalf("Expected no error but got %v", err)
    66  	}
    67  }
    68  
    69  func TestConfigLoadWithInvalidFile(t *testing.T) {
    70  	fh := createFileForTest(t)
    71  	path := fh.Name()
    72  	defer func() {
    73  		fh.Close()
    74  		os.Remove(path)
    75  	}()
    76  
    77  	// Create new config
    78  	conf, err := NewConfig()
    79  	if err != nil {
    80  		t.Fatalf("Expected no error but got %v", err)
    81  	}
    82  	// Load file source
    83  	err = conf.Load(file.NewSource(
    84  		file.WithPath(path),
    85  		file.WithPath("/i/do/not/exists.json"),
    86  	))
    87  
    88  	if err == nil {
    89  		t.Fatal("Expected error but none !")
    90  	}
    91  	if !strings.Contains(fmt.Sprintf("%v", err), "/i/do/not/exists.json") {
    92  		t.Fatalf("Expected error to contain the unexisting file but got %v", err)
    93  	}
    94  }
    95  
    96  func TestConfigMerge(t *testing.T) {
    97  	fh := createFileForIssue18(t, `{
    98    "amqp": {
    99      "host": "rabbit.platform",
   100      "port": 80
   101    },
   102    "handler": {
   103      "exchange": "springCloudBus"
   104    }
   105  }`)
   106  	path := fh.Name()
   107  	defer func() {
   108  		fh.Close()
   109  		os.Remove(path)
   110  	}()
   111  	os.Setenv("AMQP_HOST", "rabbit.testing.com")
   112  
   113  	conf, err := NewConfig()
   114  	if err != nil {
   115  		t.Fatalf("Expected no error but got %v", err)
   116  	}
   117  	if err := conf.Load(
   118  		file.NewSource(
   119  			file.WithPath(path),
   120  		),
   121  		env.NewSource(),
   122  	); err != nil {
   123  		t.Fatalf("Expected no error but got %v", err)
   124  	}
   125  
   126  	actualHost := conf.Get("amqp", "host").String("backup")
   127  	if actualHost != "rabbit.testing.com" {
   128  		t.Fatalf("Expected %v but got %v",
   129  			"rabbit.testing.com",
   130  			actualHost)
   131  	}
   132  }
   133  
   134  func equalS(t *testing.T, actual, expect string) {
   135  	if actual != expect {
   136  		t.Errorf("Expected %s but got %s", actual, expect)
   137  	}
   138  }
   139  
   140  func TestConfigWatcherDirtyOverrite(t *testing.T) {
   141  	n := runtime.GOMAXPROCS(0)
   142  	defer runtime.GOMAXPROCS(n)
   143  
   144  	runtime.GOMAXPROCS(1)
   145  
   146  	l := 100
   147  
   148  	ss := make([]source.Source, l, l)
   149  
   150  	for i := 0; i < l; i++ {
   151  		ss[i] = memory.NewSource(memory.WithJSON([]byte(fmt.Sprintf(`{"key%d": "val%d"}`, i, i))))
   152  	}
   153  
   154  	conf, _ := NewConfig()
   155  
   156  	for _, s := range ss {
   157  		_ = conf.Load(s)
   158  	}
   159  	runtime.Gosched()
   160  
   161  	for i, _ := range ss {
   162  		k := fmt.Sprintf("key%d", i)
   163  		v := fmt.Sprintf("val%d", i)
   164  		equalS(t, conf.Get(k).String(""), v)
   165  	}
   166  }