Esempio n. 1
0
func TestStopIfNotStarted(t *testing.T) {
	checker := New(time.Millisecond)
	err := checker.Stop()
	if err == nil {
		t.Error("Stopped not started checker")
	}
}
Esempio n. 2
0
func TestRemoveHook(t *testing.T) {
	checker := New(time.Millisecond)

	counter := struct {
		*sync.Mutex
		called bool
	}{
		&sync.Mutex{},
		false,
	}

	checker.AddHook("testHook", func(m map[string]bool) {
		counter.Lock()
		counter.called = true
		counter.Unlock()
	})
	checker.Start()
	time.Sleep(2 * time.Millisecond)
	counter.Lock()
	if !counter.called {
		t.Error("Counter not called")
	}
	counter.called = false
	counter.Unlock()
	checker.RemoveHook("testHook")
	time.Sleep(2 * time.Millisecond)
	counter.Lock()
	if counter.called {
		t.Error("Counter should not be called")
	}
	counter.Unlock()
	checker.Stop()
}
Esempio n. 3
0
func TestStartAlreadyStartedChecker(t *testing.T) {
	checker := New(time.Millisecond)
	err := checker.Start()
	if err != nil {
		t.Error("Start checker failed")
	}
	err = checker.Start()
	if err == nil {
		t.Error("The already started checker cannot be started")
	}
	checker.Stop()
}
Esempio n. 4
0
func TestBasicFunction(t *testing.T) {
	checker := New(time.Millisecond)
	checker.RegisterIndicator(&TestIndicator{
		&sync.Mutex{},
		true,
	})
	cond := make(chan bool, 0)

	checker.AddHook("testHook", func(m map[string]bool) {
		ok := m["test"]
		select {
		case cond <- ok:
			log.Println("Condition has been written")
		default:
			log.Println("Cant write condition")
		}
	})
	checker.Start()
	if !<-cond {
		t.Error("Helthchecker failed")
	}
	checker.Stop()
}
Esempio n. 5
0
func TestUnregister(t *testing.T) {
	checker := New(time.Millisecond)
	indicator := &TestIndicator{
		&sync.Mutex{},
		false,
	}
	checker.RegisterIndicator(indicator)
	checker.Start()
	time.Sleep(2 * time.Millisecond)
	checker.UnregisterIndicator(indicator)
	indicator.Lock()
	indicator.hasBeenCalled = false
	indicator.Unlock()
	time.Sleep(2 * time.Millisecond)

	if indicator.hasBeenCalled {
		t.Error("Unregistration not sucessfull")
	}
}