Пример #1
0
func TestLogEvent(t *testing.T) {
	srv := &Server{
		events:         make([]utils.JSONMessage, 0, 64),
		eventPublisher: utils.NewJSONMessagePublisher(),
	}

	srv.LogEvent("fakeaction", "fakeid", "fakeimage")

	listener := make(chan utils.JSONMessage)
	srv.eventPublisher.Subscribe(listener)

	srv.LogEvent("fakeaction2", "fakeid", "fakeimage")

	numEvents := len(srv.GetEvents())
	if numEvents != 2 {
		t.Fatalf("Expected 2 events, found %d", numEvents)
	}
	go func() {
		time.Sleep(200 * time.Millisecond)
		srv.LogEvent("fakeaction3", "fakeid", "fakeimage")
		time.Sleep(200 * time.Millisecond)
		srv.LogEvent("fakeaction4", "fakeid", "fakeimage")
	}()

	setTimeout(t, "Listening for events timed out", 2*time.Second, func() {
		for i := 2; i < 4; i++ {
			event := <-listener
			if event != srv.GetEvents()[i] {
				t.Fatalf("Event received it different than expected")
			}
		}
	})
}
Пример #2
0
func NewServer(eng *engine.Engine, config *daemonconfig.Config) (*Server, error) {
	daemon, err := daemon.NewDaemon(config, eng)
	if err != nil {
		return nil, err
	}
	srv := &Server{
		Eng:            eng,
		daemon:         daemon,
		pullingPool:    make(map[string]chan struct{}),
		pushingPool:    make(map[string]chan struct{}),
		events:         make([]utils.JSONMessage, 0, 64), //only keeps the 64 last events
		eventPublisher: utils.NewJSONMessagePublisher(),
	}
	daemon.SetServer(srv)
	return srv, nil
}