Esempio n. 1
0
func TestSanity(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()
	port := "44554"
	props := properties.Clone()
	props.Set("rest.port", port)

	handler := func(w http.ResponseWriter, r *http.Request) error {
		w.Write([]byte("done"))
		return nil
	}

	routes := PathHandlers{
		"/abc": MethodHandlers{
			"GET": handler,
		},
	}

	err := NewServer(props, routes)
	if err != nil {
		t.Fatalf("new server error %v", err.Error())
	}

	clientWG := sync.WaitGroup{}
	clientWG.Add(1)
	go func() {
		defer clientWG.Done()

		address := fmt.Sprintf("http://127.0.0.1:%v/abc", port)

		client := &http.Client{}
		req, err := http.NewRequest("GET", address, nil)
		if err != nil {
			t.Errorf("new request error %v", err.Error())
			return
		}

		resp, err := client.Do(req)
		if err != nil {
			t.Fatalf("response err %v", err.Error())
		}
		defer resp.Body.Close()

		bytes, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			t.Fatalf("response read all err %v", err.Error())
		}

		if string(bytes) != "done" {
			t.Fatalf("invalid response %v", string(bytes))
		}
	}()
	clientWG.Wait()

	close(registry.DoneChannel())
	registry.DoneWaitGroup().Wait()
}
Esempio n. 2
0
func TestIdleStop(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()

	err := NewServer(properties.Clone(), PathHandlers{})
	if err != nil {
		t.Fatalf("new server error %v", err.Error())
	}

	close(registry.DoneChannel())
	registry.DoneWaitGroup().Wait()
}
Esempio n. 3
0
func TestListenError(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()
	port := "13"
	props := properties.Clone()
	props.Set("rest.port", port)

	err := NewServer(props, PathHandlers{})
	if err == nil {
		t.Fatal("new server should generate error")
	}
}
Esempio n. 4
0
func TestDelayShutdownOnInitialDelay(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()

	scheduler := NewFixedDelayScheduler(100*time.Millisecond, 100*time.Millisecond)

	scheduler.Schedule(func() error {
		time.Sleep(100 * time.Millisecond)
		return nil
	})

	<-time.After(40 * time.Millisecond)

	close(registry.DoneChannel())
	registry.DoneWaitGroup().Wait()
}
Esempio n. 5
0
func TestSignal(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()

	srv := NewService()

	go func() {
		time.Sleep(300 * time.Millisecond)
		syscall.Kill(os.Getpid(), syscall.SIGTERM)
	}()

	err := srv.Forever(func() error { return nil })

	if err != nil {
		t.Error(err)
	}
}
Esempio n. 6
0
func TestDelayTaskFailure(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()

	scheduler := NewFixedDelayScheduler(0, time.Second)

	scheduler.Schedule(func() error {
		return errors.New("asd")
	})

	err := <-registry.ErrorChannel()
	close(registry.DoneChannel())
	registry.DoneWaitGroup().Wait()

	if err.Error() != "asd" {
		t.Errorf("unexpected error %v", err.Error())
	}
}
Esempio n. 7
0
func TestError(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()

	srv := NewService()
	expected := errors.New("runtime fail")

	err := srv.Forever(func() error {
		go func() {
			time.Sleep(300 * time.Millisecond)
			registry.ErrorNotify()(expected)
		}()
		return nil
	})

	if err != expected {
		t.Error(err)
	}
}
Esempio n. 8
0
func TestDelaySanity(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()

	scheduler := NewFixedDelayScheduler(0, 100*time.Millisecond)

	counter := int32(0)

	scheduler.Schedule(func() error {
		atomic.AddInt32(&counter, 1)
		time.Sleep(100 * time.Millisecond)
		return nil
	})
	<-time.After(300 * time.Millisecond)

	v := atomic.LoadInt32(&counter)
	if v != 2 {
		t.Errorf("invalid counter %v", v)
	}

	close(registry.DoneChannel())
	registry.DoneWaitGroup().Wait()
}
Esempio n. 9
0
func TestDeflateCompression(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()
	port := "44559"
	props := properties.Clone()
	props.Set("rest.port", port)
	props.Set("rest.compression", "true")

	handler := func(w http.ResponseWriter, r *http.Request) error {
		w.Write([]byte("done"))
		return nil
	}

	routes := PathHandlers{
		"/compression": MethodHandlers{
			"GET": handler,
		},
	}

	err := NewServer(props, routes)
	if err != nil {
		t.Fatalf("new server error %v", err.Error())
	}

	clientWG := sync.WaitGroup{}
	clientWG.Add(1)
	go func() {
		defer clientWG.Done()

		address := fmt.Sprintf("http://127.0.0.1:%v/compression", port)

		client := &http.Client{}
		req, err := http.NewRequest("GET", address, nil)
		if err != nil {
			t.Errorf("new request error %v", err.Error())
			return
		}
		req.Header.Add("Accept-Encoding", "deflate,gzip")

		resp, err := client.Do(req)
		if err != nil {
			t.Fatalf("response err %v", err.Error())
		}
		defer resp.Body.Close()

		encoding := resp.Header.Get(http.CanonicalHeaderKey("Content-Encoding"))
		if encoding != "deflate" {
			t.Fatalf("invalid response encoding %v", encoding)
		}

		bytes, err := ioutil.ReadAll(flate.NewReader(resp.Body))
		if err != nil {
			t.Fatalf("response read all err %v", err.Error())
		}

		if string(bytes) != "done" {
			t.Fatalf("invalid response %v", string(bytes))
		}
	}()
	clientWG.Wait()

	close(registry.DoneChannel())
	registry.DoneWaitGroup().Wait()
}
Esempio n. 10
0
func TestNilResponse(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()
	port := "44557"
	props := properties.Clone()
	props.Set("rest.port", port)

	handler := func(w http.ResponseWriter, r *http.Request) error {
		WriteResponse(w, nil, http.StatusConflict)
		return nil
	}

	routes := PathHandlers{
		"/nil": MethodHandlers{
			"GET": handler,
		},
	}

	err := NewServer(props, routes)
	if err != nil {
		t.Fatalf("new server error %v", err.Error())
	}

	clientWG := sync.WaitGroup{}
	clientWG.Add(1)
	go func() {
		defer clientWG.Done()

		address := fmt.Sprintf("http://127.0.0.1:%v/nil", port)

		client := &http.Client{}
		req, err := http.NewRequest("GET", address, nil)
		if err != nil {
			t.Fatalf("new request error %v", err.Error())
			return
		}

		resp, err := client.Do(req)
		if err != nil {
			t.Fatalf("response err %v", err.Error())
		}
		defer resp.Body.Close()

		if resp.StatusCode != http.StatusConflict {
			t.Fatalf("invalid response status code %v", resp.StatusCode)
		}

		var responseContent string
		err = json.NewDecoder(resp.Body).Decode(&responseContent)
		if err != nil {
			t.Fatalf("response decode error %v", err.Error())
		}

		if responseContent != http.StatusText(http.StatusConflict) {
			t.Fatalf("invalid response %v", responseContent)
		}
	}()
	clientWG.Wait()

	close(registry.DoneChannel())
	registry.DoneWaitGroup().Wait()
}
Esempio n. 11
0
func TestErrorInHandler(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()
	port := "44556"
	props := properties.Clone()
	props.Set("rest.port", port)

	errorMessage := "f951a2344b8349ac222f02b4646ad504efc759e9"

	handler := func(w http.ResponseWriter, r *http.Request) error {
		return errors.New(errorMessage)
	}

	routes := PathHandlers{
		"/err": MethodHandlers{
			"GET": handler,
		},
	}

	err := NewServer(props, routes)
	if err != nil {
		t.Fatalf("new server error %v", err.Error())
	}

	clientWG := sync.WaitGroup{}
	clientWG.Add(1)
	go func() {
		defer clientWG.Done()

		address := fmt.Sprintf("http://127.0.0.1:%v/err", port)

		client := &http.Client{}
		req, err := http.NewRequest("GET", address, nil)
		if err != nil {
			t.Fatalf("new request error %v", err.Error())
			return
		}

		resp, err := client.Do(req)
		if err != nil {
			t.Fatalf("response err %v", err.Error())
		}
		defer resp.Body.Close()

		if resp.StatusCode != http.StatusInternalServerError {
			t.Fatalf("invalid response status code %v", resp.StatusCode)
		}

		var responseContent string
		err = json.NewDecoder(resp.Body).Decode(&responseContent)
		if err != nil {
			t.Fatalf("response decode error %v", err.Error())
		}

		if responseContent != errorMessage {
			t.Fatalf("invalid response %v", responseContent)
		}
	}()
	clientWG.Wait()

	close(registry.DoneChannel())
	registry.DoneWaitGroup().Wait()
}