Ejemplo n.º 1
0
func TestMultipleRegistrations(t *testing.T) {
	testcases := []struct {
		name    string         // component
		checker health.Checker // health checker
	}{
		{"component-alive", healthy},
		{"component-up", healthy},
		{"component-failed", failed},
		{"component-dead", failed},
		{"component-ok", healthy},
	}

	for _, tc := range testcases {
		health.Register(tc.name, tc.checker)
	}
	assert.Len(t, health.Components(), len(testcases)) // all registered

	all := health.RunChecks()

	for _, tc := range testcases {
		assert.Contains(t, health.Components(), tc.name)
		_, found := all[tc.name]
		assert.True(t, found)
		assert.EqualValues(t, all[tc.name], tc.checker.Check(), tc.name)
	}

	teardown()
}
Ejemplo n.º 2
0
func TestDeregisterHealthCheck(t *testing.T) {
	health.Register(COMPONENT, healthy)
	health.Unregister(COMPONENT)
	assert.Len(t, health.Components(), 0)
	assert.NotContains(t, health.Components(), COMPONENT)
	all := health.RunChecks()
	assert.Len(t, all, 0)

	teardown()
}
Ejemplo n.º 3
0
func TestRegister(t *testing.T) {
	health.Register(COMPONENT, healthy)
	assert.Len(t, health.Components(), 1)
	assert.Contains(t, health.Components(), COMPONENT)
	all := health.RunChecks()

	status, found := all[COMPONENT]
	assert.True(t, found)
	assert.EqualValues(t, status, healthy.Check())

	teardown()
}
Ejemplo n.º 4
0
func TestRegisterDuplicateHealthCheck(t *testing.T) {
	health.Register(COMPONENT, healthy)
	health.RegisterFunc(COMPONENT, alwaysFailing) // replace with failing health check
	assert.Len(t, health.Components(), 1)
	assert.Contains(t, health.Components(), COMPONENT)
	all := health.RunChecks()

	status, found := all[COMPONENT]
	assert.True(t, found)
	assert.EqualValues(t, status, alwaysFailing())

	teardown()
}
Ejemplo n.º 5
0
func TestHealthCheckExecuteUnhealthy(t *testing.T) {
	health.Register(COMPONENT, &stubHealthCheck{healthy: false})

	checks := health.RunChecks()

	for component, hc := range checks {
		assert.Equal(t, component, COMPONENT)
		assert.False(t, hc.Healthy)
		assert.Equal(t, FAILING, hc.Properties["message"])
	}

	teardown()
}
Ejemplo n.º 6
0
func TestHealthCheckExecuteHealthy(t *testing.T) {
	health.Register(COMPONENT, &stubHealthCheck{healthy: true})

	checks := health.RunChecks()

	for component, hc := range checks {
		assert.Equal(t, component, COMPONENT)
		assert.True(t, hc.Healthy)
		assert.Empty(t, hc.Properties["cause"])
	}

	teardown()
}
Ejemplo n.º 7
0
// New creates and initializes a new cluster with the given configuration.
// Nil argument results with default values for the configuration.
func New(conf *Config) (Cluster, error) {
	conf = defaultize(conf)

	b, err := newBackend(conf)
	if err != nil {
		return nil, err
	}

	m := newMembership(b, conf.TTL, conf.ScanInterval)
	c := &cluster{backend: b, membership: m, conf: conf}

	m.StartMonitoring()

	hc := newHealthChecker(m, conf.Size)
	health.Register(module, hc)

	return c, nil
}
Ejemplo n.º 8
0
// New - Create a new replication instance
func New(conf *Config) (Replication, error) {
	var lentry = logging.GetLogger(module)

	if conf == nil {
		err := fmt.Errorf("Nil conf")
		lentry.WithFields(log.Fields{
			"error": err,
		}).Error("Failed to create replication server")

		return nil, err
	}

	if conf.Membership == nil || conf.Registrator == nil {
		err := fmt.Errorf("Nil cluster membership and/or registrator")
		lentry.WithFields(log.Fields{
			"error": err,
		}).Error("Failed to create replication server")

		return nil, err
	}

	// Make sure that the listening port is free
	address := fmt.Sprintf("%s:%d", conf.Registrator.Self().IP(), conf.Registrator.Self().Port())
	listener, err := net.Listen("tcp", address)
	if err != nil {
		lentry.WithFields(log.Fields{
			"error": err,
		}).Error("Failed to create replication server")

		return nil, err
	}

	tr := &http.Transport{MaxIdleConnsPerHost: 1}
	hc := &http.Client{Transport: tr}
	logger := lentry.WithFields(log.Fields{"Member-ID": conf.Registrator.Self().ID()})

	// Instantiate a server
	s := &server{
		listener:       listener,
		httpclient:     hc,
		transport:      tr,
		broadcast:      channels.NewChannelTimeout(512),
		repair:         channels.NewChannelTimeout(512),
		newPeers:       make(chan *peer),
		closingPeers:   make(chan *peer, 8),
		peers:          make(map[cluster.MemberID]*peer),
		mux:            http.NewServeMux(),
		replicators:    make(map[auth.Namespace]*replicator),
		clients:        make(map[cluster.MemberID]*client),
		selfID:         conf.Registrator.Self().ID(),
		membership:     conf.Membership,
		registrator:    conf.Registrator,
		notifyChannel:  make(chan *InMessage, 512),
		syncReqChannel: make(chan chan []byte, 8),
		health:         newHealthChecker(),
		done:           make(chan struct{}),
		logger:         logger,
	}

	health.Register(module, s.health)

	logger.Info("Replication server created")
	return s, nil
}