...

Source file src/github.com/go-kit/kit/sd/dnssrv/instancer_test.go

Documentation: github.com/go-kit/kit/sd/dnssrv

     1  package dnssrv
     2  
     3  import (
     4  	"net"
     5  	"sync/atomic"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/go-kit/kit/sd"
    10  	"github.com/go-kit/log"
    11  )
    12  
    13  var _ sd.Instancer = (*Instancer)(nil) // API check
    14  
    15  func TestRefresh(t *testing.T) {
    16  	name := "some.service.internal"
    17  
    18  	ticker := time.NewTicker(time.Second)
    19  	ticker.Stop()
    20  	tickc := make(chan time.Time)
    21  	ticker.C = tickc
    22  
    23  	var lookups uint64
    24  	records := []*net.SRV{}
    25  	lookup := func(service, proto, name string) (string, []*net.SRV, error) {
    26  		t.Logf("lookup(%q, %q, %q)", service, proto, name)
    27  		atomic.AddUint64(&lookups, 1)
    28  		return "cname", records, nil
    29  	}
    30  
    31  	instancer := NewInstancerDetailed(name, ticker, lookup, log.NewNopLogger())
    32  	defer instancer.Stop()
    33  
    34  	// First lookup, empty
    35  	state := instancer.cache.State()
    36  	if state.Err != nil {
    37  		t.Error(state.Err)
    38  	}
    39  	if want, have := 0, len(state.Instances); want != have {
    40  		t.Errorf("want %d, have %d", want, have)
    41  	}
    42  	if want, have := uint64(1), atomic.LoadUint64(&lookups); want != have {
    43  		t.Errorf("want %d, have %d", want, have)
    44  	}
    45  
    46  	// Load some records and lookup again
    47  	records = []*net.SRV{
    48  		{Target: "1.0.0.1", Port: 1001},
    49  		{Target: "1.0.0.2", Port: 1002},
    50  		{Target: "1.0.0.3", Port: 1003},
    51  	}
    52  	tickc <- time.Now()
    53  
    54  	// There is a race condition where the instancer.State call below
    55  	// invokes the cache before it is updated by the tick above.
    56  	// TODO(pb): solve by running the read through the loop goroutine.
    57  	time.Sleep(100 * time.Millisecond)
    58  
    59  	state = instancer.cache.State()
    60  	if state.Err != nil {
    61  		t.Error(state.Err)
    62  	}
    63  	if want, have := 3, len(state.Instances); want != have {
    64  		t.Errorf("want %d, have %d", want, have)
    65  	}
    66  	if want, have := uint64(2), atomic.LoadUint64(&lookups); want != have {
    67  		t.Errorf("want %d, have %d", want, have)
    68  	}
    69  }
    70  
    71  func TestIssue892(t *testing.T) {
    72  	ticker := time.NewTicker(time.Second)
    73  	ticker.Stop()
    74  	tickc := make(chan time.Time)
    75  	ticker.C = tickc
    76  
    77  	records := []*net.SRV{
    78  		{Target: "1.0.0.1", Port: 80},
    79  		{Target: "1.0.0.2", Port: 0},
    80  		{Target: "1.0.0.3", Port: 80},
    81  	}
    82  
    83  	lookup := func(service, proto, name string) (string, []*net.SRV, error) {
    84  		return "cname", records, nil
    85  	}
    86  
    87  	instancer := NewInstancerDetailed("name", ticker, lookup, log.NewNopLogger())
    88  	defer instancer.Stop()
    89  
    90  	tickc <- time.Now()
    91  	time.Sleep(100 * time.Millisecond)
    92  
    93  	if want, have := ErrPortZero, instancer.cache.State().Err; want != have {
    94  		t.Fatalf("want %v, have %v", want, have)
    95  	}
    96  }
    97  
    98  type nopCloser struct{}
    99  
   100  func (nopCloser) Close() error { return nil }
   101  

View as plain text