Exemple #1
0
func run(opts *Options) {
	// Dial the selected event backend.
	ep, err := dialEventProvider(opts)
	if err != nil {
		log.Fatal(err)
	}

	defer ep.Close()

	// Start a WatchService which coordiates access to watchdb.
	ws, err := worker.Start()
	if err != nil {
		log.Fatal(err)
	}

	defer ws.Stop()

	// Start the HTTP servers.
	for _, conf := range servers(ws, ep, opts) {
		srv, err := conf.Start()
		if err != nil {
			log.Fatal(err)
		}

		defer srv.Stop()
	}

	// Wait for OS signals.
	await(opts)
}
func TestCreateWatch(t *testing.T) {
	ws, err := worker.Start()
	if err != nil {
		t.Error(err)
		return
	}

	defer ws.Stop()

	mux := NewWatchHandler(ws)

	s, err := StartHttpServer(ListenAddr, mux)
	if err != nil {
		t.Error(err)
		return
	}

	defer s.Stop()

	id, err := postWatch(t, "http://127.0.0.1:9001/watches")
	if err != nil {
		t.Error(err)
		return
	}

	const WATCH_ID = 1

	if id != WATCH_ID {
		t.Errorf("expected: %d, got: %d", WATCH_ID, id)
		return
	}

	db := ws.(UnsafeAccessor).UnsafeGetWatchProvider()
	if !db.Contains(WATCH_ID) {
		t.Errorf("expected the watchdb to contain a watch with id=%d", WATCH_ID)
		return
	}
}
func TestMatchHandler(t *testing.T) {
	const (
		LISTEN_ADDR = ":9002"
		WATCHED_URL = "http://127.0.0.1:9002/foo"
		WATCH_BODY  = "hello, world"
		DSN         = "dummy-service"
	)

	ws, err := worker.Start()
	if err != nil {
		t.Error(err)
		return
	}

	defer ws.Stop()

	echo := &fakeResponse{body: WATCH_BODY}
	w := watchdb.NewWatch(DSN,
		"/foo", "GET", "redis:watch:1234", echo)

	_, _, err = ws.Add(w)
	if err != nil {
		t.Error(err)
		return
	}

	mx := watchdb.NewMatchExpr(DSN, "/foo", "GET")
	m, _ := ws.Match(mx)
	if !m.IsMatch() {
		t.Errorf("watchdb doesn't recognize the watch")
		return
	}

	ep := eventdb.NopEventProvider()

	mux := NewMatchHandler(DSN, ws, ep)

	s, err := StartHttpServer(LISTEN_ADDR, mux)
	if err != nil {
		t.Error(err)
		return
	}

	defer s.Stop()

	resp, err := http.Get(WATCHED_URL)
	if err != nil {
		t.Error(err)
		return
	}

	if resp.StatusCode != 200 {
		t.Errorf("expected: %d, got: %d", 200, resp.StatusCode)
		return
	}

	bufr := bufio.NewReader(resp.Body)
	yy, err := ioutil.ReadAll(bufr)
	resp.Body.Close()
	if err != nil {
		t.Error(err)
		return
	}

	str := string(yy)
	if str != WATCH_BODY {
		t.Errorf("unexpected: %s", str)
		t.Errorf("expected: \"%s\", got: \"%s\"", WATCH_BODY, str)

		// Fprintln adds a '\n', oops.
		spew.Dump(yy)
		spew.Dump(str)
		return
	}
}