Codebase list golang-github-go-kit-kit / 72a93ad sd / eureka / registrar_test.go
72a93ad

Tree @72a93ad (Download .tar.gz)

registrar_test.go @72a93adraw · history · blame

package eureka

import (
	"testing"
	"time"

	fargo "github.com/hudl/fargo"
)

func TestRegistrar(t *testing.T) {
	client := &testClient{
		instances:    []*fargo.Instance{},
		errHeartbeat: errTest,
	}

	r := NewRegistrar(client, instanceTest1, loggerTest)
	if want, have := 0, len(client.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// Not registered.
	r.Deregister()
	if want, have := 0, len(client.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// Register.
	r.Register()
	if want, have := 1, len(client.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// Deregister.
	r.Deregister()
	if want, have := 0, len(client.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// Already registered.
	r.Register()
	if want, have := 1, len(client.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
	r.Register()
	if want, have := 1, len(client.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// Wait for a heartbeat failure.
	time.Sleep(time.Second)
	if want, have := 1, len(client.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
	r.Deregister()
	if want, have := 0, len(client.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}