Пример #1
0
func TestPutWithBackend(t *testing.T) {
	uuid, _ := util.NewUUID()

	storage, _, put := fileServer(uuid)
	defer storage.Close()

	baseServer.StorageBaseURL = storage.URL
	defer func() {
		baseServer.StorageBaseURL = ""
	}()

	server := httptest.NewServer(baseServer.router())
	defer server.Close()

	transport := &http.Transport{}
	client := &http.Client{Transport: transport}

	registrar := broker.NewRedisRegistrar()
	registrar.Register(uuid)

	// uuid = curl -XPUT <url>/streams/1/2/3
	request, _ := http.NewRequest("POST", server.URL+"/streams/"+uuid, bytes.NewReader([]byte("hello world")))
	request.TransferEncoding = []string{"chunked"}
	resp, err := client.Do(request)
	defer resp.Body.Close()
	assert.Nil(t, err)
	assert.Equal(t, resp.StatusCode, 200)
	assert.Equal(t, <-put, []byte("hello world"))
}
Пример #2
0
func setup() string {
	registrar := NewRedisRegistrar()
	uuid, _ := util.NewUUID()
	registrar.Register(uuid)

	return uuid
}
Пример #3
0
func TestPubSub(t *testing.T) {
	server := httptest.NewServer(baseServer.router())
	defer server.Close()

	data := [][]byte{
		[]byte{'h', 'e', 'l', 'l', 'o'},
		[]byte{0x1f, 0x8b, 0x08, 0x00, 0x3f, 0x6b, 0xe1, 0x53, 0x00, 0x03, 0xed, 0xce, 0xb1, 0x0a, 0xc2, 0x30},
		bytes.Repeat([]byte{'0'}, 32769),
	}

	client := &http.Client{Transport: &http.Transport{}}
	for _, expected := range data {
		uuid, _ := util.NewUUID()
		url := server.URL + "/streams/" + uuid

		// curl -XPUT <url>/streams/<uuid>
		request, _ := http.NewRequest("PUT", url, nil)
		resp, err := client.Do(request)
		defer resp.Body.Close()
		assert.Nil(t, err)

		done := make(chan bool)

		go func() {
			// curl <url>/streams/<uuid>
			// -- waiting for publish to arrive
			resp, err := http.Get(server.URL + "/streams/" + uuid)
			defer resp.Body.Close()
			assert.Nil(t, err)

			body, _ := ioutil.ReadAll(resp.Body)
			assert.Equal(t, body, expected)

			done <- true
		}()

		// curl -XPOST -H "Transfer-Encoding: chunked" -d "hello" <url>/streams/<uuid>
		req, _ := http.NewRequest("POST", server.URL+"/streams/"+uuid, bytes.NewReader(expected))
		req.TransferEncoding = []string{"chunked"}
		r, err := client.Do(req)
		r.Body.Close()
		assert.Nil(t, err)

		<-done

		// Read the whole response after the publisher has
		// completed. The mechanics of this is different in that
		// most of the content will be replayed instead of received
		// in chunks as they arrive.
		resp, err = http.Get(server.URL + "/streams/" + uuid)
		defer resp.Body.Close()
		assert.Nil(t, err)
		assert.Equal(t, 200, resp.StatusCode)

		body, _ := ioutil.ReadAll(resp.Body)
		assert.Equal(t, body, expected)
	}
}
Пример #4
0
func newReaderWriter() (io.ReadCloser, io.WriteCloser) {
	registrar := NewRedisRegistrar()
	uuid, _ := util.NewUUID()
	registrar.Register(uuid)
	r, _ := NewReader(uuid)
	w, _ := NewWriter(uuid)

	return r, w
}
Пример #5
0
func TestPubNotRegistered(t *testing.T) {
	streamID, _ := util.NewUUID()
	request, _ := http.NewRequest("POST", "/streams/"+streamID, nil)
	request.TransferEncoding = []string{"chunked"}
	response := httptest.NewRecorder()

	baseServer.publish(response, request)

	assert.Equal(t, response.Code, http.StatusNotFound)
}
Пример #6
0
func Test410(t *testing.T) {
	streamID, _ := util.NewUUID()
	request, _ := http.NewRequest("GET", "/streams/"+streamID, nil)
	response := httptest.NewRecorder()

	baseServer.subscribe(response, request)

	assert.Equal(t, response.Code, http.StatusNotFound)
	assert.Equal(t, response.Body.String(), "Channel is not registered.\n")
}
Пример #7
0
func TestPublisherReconnect(t *testing.T) {
	server := httptest.NewServer(baseServer.router())
	defer server.Close()

	client := &http.Client{Transport: &http.Transport{}}

	uuid, _ := util.NewUUID()
	url := server.URL + "/streams/" + uuid
	// curl -XPUT <url>/streams/<uuid>
	request, _ := http.NewRequest("PUT", url, nil)
	resp, err := client.Do(request)
	defer resp.Body.Close()
	assert.Nil(t, err)

	done := make(chan bool)

	writer, err := broker.NewWriter(uuid)
	assert.Nil(t, err)
	_, err = writer.Write([]byte("hello"))
	assert.Nil(t, err)
	defer writer.Close()

	go func() {
		// curl <url>/streams/<uuid>
		// -- waiting for publish to arrive
		resp, err := http.Get(server.URL + "/streams/" + uuid)
		defer resp.Body.Close()
		assert.Nil(t, err)

		body, _ := ioutil.ReadAll(resp.Body)
		assert.Equal(t, body, []byte("hello world"))

		done <- true
	}()

	// curl -XPOST -H "Transfer-Encoding: chunked" -d "hello" <url>/streams/<uuid>
	req, _ := http.NewRequest("POST", server.URL+"/streams/"+uuid, bytes.NewReader([]byte("hello world")))
	req.TransferEncoding = []string{"chunked"}
	r, err := client.Do(req)
	r.Body.Close()
	assert.Nil(t, err)

	<-done

	// Read the whole response after the publisher has
	// completed. The mechanics of this is different in that
	// most of the content will be replayed instead of received
	// in chunks as they arrive.
	resp, err = http.Get(server.URL + "/streams/" + uuid)
	defer resp.Body.Close()
	assert.Nil(t, err)

	body, _ := ioutil.ReadAll(resp.Body)
	assert.Equal(t, body, []byte("hello world"))
}
Пример #8
0
func requestId(r *http.Request) (id string) {
	if id = r.Header.Get("Request-Id"); id == "" {
		id = r.Header.Get("X-Request-Id")
	}

	if id == "" {
		// In the event of a rare case where uuid
		// generation fails, it's probably more
		// desirable to continue as is with an empty
		// request_id than to bubble the error up the
		// stack.
		uuid, _ := util.NewUUID()
		id = string(uuid)
	}

	return id
}
Пример #9
0
func TestPubClosed(t *testing.T) {
	uuid, _ := util.NewUUID()

	registrar := broker.NewRedisRegistrar()
	err := registrar.Register(uuid)
	assert.Nil(t, err)
	writer, err := broker.NewWriter(uuid)
	assert.Nil(t, err)
	writer.Close()

	request, _ := http.NewRequest("POST", "/streams/"+uuid, nil)
	request.TransferEncoding = []string{"chunked"}
	response := httptest.NewRecorder()

	baseServer.publish(response, request)

	assert.Equal(t, response.Code, http.StatusNotFound)
}
Пример #10
0
Файл: server.go Проект: cyx/busl
func mkstream(w http.ResponseWriter, _ *http.Request) {
	registrar := broker.NewRedisRegistrar()
	uuid, err := util.NewUUID()
	if err != nil {
		http.Error(w, "Unable to create stream. Please try again.", http.StatusServiceUnavailable)
		rollbar.Error(rollbar.ERR, fmt.Errorf("unable to create new uuid for stream: %#v", err))
		util.CountWithData("mkstream.create.fail", 1, "error=%s", err)
		return
	}

	if err := registrar.Register(uuid); err != nil {
		http.Error(w, "Unable to create stream. Please try again.", http.StatusServiceUnavailable)
		rollbar.Error(rollbar.ERR, fmt.Errorf("unable to register stream: %#v", err))
		util.CountWithData("mkstream.create.fail", 1, "error=%s", err)
		return
	}

	util.Count("mkstream.create.success")
	io.WriteString(w, string(uuid))
}
Пример #11
0
func TestSubGoneWithBackend(t *testing.T) {
	uuid, _ := util.NewUUID()

	storage, get, _ := fileServer(uuid)
	defer storage.Close()

	*util.StorageBaseURL = storage.URL
	defer func() {
		*util.StorageBaseURL = baseURL
	}()

	server := httptest.NewServer(app())
	defer server.Close()

	get <- []byte("hello world")

	resp, err := http.Get(server.URL + "/streams/" + uuid)
	defer resp.Body.Close()
	assert.Nil(t, err)

	body, _ := ioutil.ReadAll(resp.Body)
	assert.Equal(t, body, []byte("hello world"))
}
Пример #12
0
func newRegUUID() (*RedisRegistrar, string) {
	reg := NewRedisRegistrar()
	uuid, _ := util.NewUUID()

	return reg, uuid
}
Пример #13
0
func TestPubSubSSE(t *testing.T) {
	server := httptest.NewServer(baseServer.router())
	defer server.Close()

	data := []struct {
		offset int
		input  string
		output string
	}{
		{0, "hello", "id: 5\ndata: hello\n\n"},
		{0, "hello\n", "id: 6\ndata: hello\ndata: \n\n"},
		{0, "hello\nworld", "id: 11\ndata: hello\ndata: world\n\n"},
		{0, "hello\nworld\n", "id: 12\ndata: hello\ndata: world\ndata: \n\n"},
		{1, "hello\nworld\n", "id: 12\ndata: ello\ndata: world\ndata: \n\n"},
		{6, "hello\nworld\n", "id: 12\ndata: world\ndata: \n\n"},
		{11, "hello\nworld\n", "id: 12\ndata: \ndata: \n\n"},
		{12, "hello\nworld\n", ""},
	}

	client := &http.Client{Transport: &http.Transport{}}

	for _, testdata := range data {
		uuid, _ := util.NewUUID()
		url := server.URL + "/streams/" + uuid

		// curl -XPUT <url>/streams/<uuid>
		request, _ := http.NewRequest("PUT", url, nil)
		resp, err := client.Do(request)
		defer resp.Body.Close()
		assert.Nil(t, err)

		done := make(chan bool)

		// curl -XPOST -H "Transfer-Encoding: chunked" -d "hello" <url>/streams/<uuid>
		req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte(testdata.input)))
		req.TransferEncoding = []string{"chunked"}

		r, err := client.Do(req)
		assert.Nil(t, err)
		r.Body.Close()

		go func() {
			request, _ := http.NewRequest("GET", url, nil)
			request.Header.Add("Accept", "text/event-stream")
			request.Header.Add("Last-Event-Id", strconv.Itoa(testdata.offset))

			// curl <url>/streams/<uuid>
			// -- waiting for publish to arrive
			resp, err := client.Do(request)
			defer resp.Body.Close()
			assert.Nil(t, err)

			body, _ := ioutil.ReadAll(resp.Body)
			assert.Equal(t, body, []byte(testdata.output))

			if len(body) == 0 {
				assert.Equal(t, resp.StatusCode, http.StatusNoContent)
			}

			done <- true
		}()

		<-done
	}
}