diff --git a/admin/uAdmin_test.go b/admin/uAdmin_test.go new file mode 100644 index 0000000..22d1745 --- /dev/null +++ b/admin/uAdmin_test.go @@ -0,0 +1,191 @@ +package admin + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + "testing" + + "github.com/buaazp/uq/queue" + "github.com/buaazp/uq/store" + . "github.com/smartystreets/goconvey/convey" +) + +var ( + storage store.Storage + messageQueue queue.MessageQueue + entrance AdminServer + client *http.Client +) + +func init() { + client = new(http.Client) +} + +func TestNewAdmin(t *testing.T) { + Convey("Test New Admin", t, func() { + var err error + storage, err = store.NewMemStore() + So(err, ShouldBeNil) + So(storage, ShouldNotBeNil) + messageQueue, err = queue.NewUnitedQueue(storage, "127.0.0.1", 8800, nil, "uq") + So(err, ShouldBeNil) + So(messageQueue, ShouldNotBeNil) + + entrance, err = NewAdminServer("0.0.0.0", 8800, messageQueue) + So(err, ShouldBeNil) + So(entrance, ShouldNotBeNil) + + go func() { + entrance.ListenAndServe() + }() + }) +} + +func TestAdminAdd(t *testing.T) { + Convey("Test Admin Add Api", t, func() { + bf := bytes.NewBufferString("topic=foo") + body := ioutil.NopCloser(bf) + req, err := http.NewRequest( + "PUT", + "http://127.0.0.1:8800/v1/queues", + body, + ) + So(err, ShouldBeNil) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusCreated) + + bf = bytes.NewBufferString("topic=foo&line=x&recycle=10s") + body = ioutil.NopCloser(bf) + req, err = http.NewRequest( + "PUT", + "http://127.0.0.1:8800/v1/queues", + body, + ) + So(err, ShouldBeNil) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + resp, err = client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusCreated) + }) +} + +func TestAdminPush(t *testing.T) { + Convey("Test Admin Push Api", t, func() { + bf := bytes.NewBufferString("value=1") + body := ioutil.NopCloser(bf) + req, err := http.NewRequest( + "POST", + "http://127.0.0.1:8800/v1/queues/foo", + body, + ) + So(err, ShouldBeNil) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusNoContent) + }) +} + +func TestAdminPop(t *testing.T) { + Convey("Test Admin Pop Api", t, func() { + req, err := http.NewRequest( + "GET", + "http://127.0.0.1:8800/v1/queues/foo/x", + nil, + ) + So(err, ShouldBeNil) + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusOK) + + body, err := ioutil.ReadAll(resp.Body) + So(err, ShouldBeNil) + id := resp.Header.Get("X-UQ-ID") + So(id, ShouldEqual, "foo/x/0") + msg := string(body) + So(msg, ShouldEqual, "1") + }) +} + +func TestAdminConfirm(t *testing.T) { + Convey("Test Admin Confirm Api", t, func() { + req, err := http.NewRequest( + "DELETE", + "http://127.0.0.1:8800/v1/queues/foo/x/0", + nil, + ) + So(err, ShouldBeNil) + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusNoContent) + }) +} + +func TestAdminStat(t *testing.T) { + Convey("Test Admin Stat Api", t, func() { + req, err := http.NewRequest( + "GET", + "http://127.0.0.1:8800/v1/admin/stat/foo/x", + nil, + ) + So(err, ShouldBeNil) + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusOK) + + body, err := ioutil.ReadAll(resp.Body) + So(err, ShouldBeNil) + var qs queue.QueueStat + err = json.Unmarshal(body, &qs) + So(err, ShouldBeNil) + So(qs.Name, ShouldEqual, "foo/x") + }) +} + +func TestAdminEmpty(t *testing.T) { + Convey("Test Admin Empty Api", t, func() { + req, err := http.NewRequest( + "DELETE", + "http://127.0.0.1:8800/v1/admin/empty/foo/x", + nil, + ) + So(err, ShouldBeNil) + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusNoContent) + }) +} + +func TestAdminRemove(t *testing.T) { + Convey("Test Admin Remove Api", t, func() { + req, err := http.NewRequest( + "DELETE", + "http://127.0.0.1:8800/v1/admin/rm/foo/x", + nil, + ) + So(err, ShouldBeNil) + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusNoContent) + }) +} + +func TestCloseAdmin(t *testing.T) { + Convey("Test Close Admin", t, func() { + entrance.Stop() + messageQueue.Close() + messageQueue = nil + storage = nil + }) +} diff --git a/entry/httpEntry_test.go b/entry/httpEntry_test.go new file mode 100644 index 0000000..55abce2 --- /dev/null +++ b/entry/httpEntry_test.go @@ -0,0 +1,190 @@ +package entry + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + "testing" + + "github.com/buaazp/uq/queue" + "github.com/buaazp/uq/store" + . "github.com/smartystreets/goconvey/convey" +) + +var ( + storage store.Storage + messageQueue queue.MessageQueue + entrance Entrance + client *http.Client +) + +func init() { + client = new(http.Client) +} + +func TestNewHttpEntry(t *testing.T) { + Convey("Test New HTTP Entry", t, func() { + var err error + storage, err = store.NewMemStore() + So(err, ShouldBeNil) + So(storage, ShouldNotBeNil) + messageQueue, err = queue.NewUnitedQueue(storage, "127.0.0.1", 8801, nil, "uq") + So(err, ShouldBeNil) + So(messageQueue, ShouldNotBeNil) + + entrance, err = NewHttpEntry("0.0.0.0", 8801, messageQueue) + So(err, ShouldBeNil) + So(entrance, ShouldNotBeNil) + + go func() { + entrance.ListenAndServe() + }() + }) +} + +func TestHttpAdd(t *testing.T) { + Convey("Test Http Add Api", t, func() { + bf := bytes.NewBufferString("topic=foo") + body := ioutil.NopCloser(bf) + req, err := http.NewRequest( + "PUT", + "http://127.0.0.1:8801/v1/queues", + body, + ) + So(err, ShouldBeNil) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusCreated) + + bf = bytes.NewBufferString("topic=foo&line=x&recycle=10s") + body = ioutil.NopCloser(bf) + req, err = http.NewRequest( + "PUT", + "http://127.0.0.1:8801/v1/queues", + body, + ) + So(err, ShouldBeNil) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + resp, err = client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusCreated) + }) +} + +func TestHttpPush(t *testing.T) { + Convey("Test Http Push Api", t, func() { + bf := bytes.NewBufferString("value=1") + body := ioutil.NopCloser(bf) + req, err := http.NewRequest( + "POST", + "http://127.0.0.1:8801/v1/queues/foo", + body, + ) + So(err, ShouldBeNil) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusNoContent) + }) +} + +func TestHttpPop(t *testing.T) { + Convey("Test Http Pop Api", t, func() { + req, err := http.NewRequest( + "GET", + "http://127.0.0.1:8801/v1/queues/foo/x", + nil, + ) + So(err, ShouldBeNil) + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusOK) + + body, err := ioutil.ReadAll(resp.Body) + So(err, ShouldBeNil) + id := resp.Header.Get("X-UQ-ID") + So(id, ShouldEqual, "foo/x/0") + msg := string(body) + So(msg, ShouldEqual, "1") + }) +} + +func TestHttpConfirm(t *testing.T) { + Convey("Test Http Confirm Api", t, func() { + req, err := http.NewRequest( + "DELETE", + "http://127.0.0.1:8801/v1/queues/foo/x/0", + nil, + ) + So(err, ShouldBeNil) + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusNoContent) + }) +} + +func TestHttpStat(t *testing.T) { + Convey("Test Http Stat Api", t, func() { + req, err := http.NewRequest( + "GET", + "http://127.0.0.1:8801/v1/admin/stat/foo/x", + nil, + ) + So(err, ShouldBeNil) + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusOK) + + body, err := ioutil.ReadAll(resp.Body) + So(err, ShouldBeNil) + var qs queue.QueueStat + err = json.Unmarshal(body, &qs) + So(err, ShouldBeNil) + So(qs.Name, ShouldEqual, "foo/x") + }) +} + +func TestHttpEmpty(t *testing.T) { + Convey("Test Http Empty Api", t, func() { + req, err := http.NewRequest( + "DELETE", + "http://127.0.0.1:8801/v1/admin/empty/foo/x", + nil, + ) + So(err, ShouldBeNil) + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusNoContent) + }) +} + +func TestHttpRemove(t *testing.T) { + Convey("Test Http Remove Api", t, func() { + req, err := http.NewRequest( + "DELETE", + "http://127.0.0.1:8801/v1/admin/rm/foo/x", + nil, + ) + So(err, ShouldBeNil) + + resp, err := client.Do(req) + So(err, ShouldBeNil) + So(resp.StatusCode, ShouldEqual, http.StatusNoContent) + }) +} + +func TestCloseHttpEntry(t *testing.T) { + Convey("Test Close Http Entry", t, func() { + entrance.Stop() + messageQueue = nil + storage = nil + }) +} diff --git a/entry/mcEntry_test.go b/entry/mcEntry_test.go new file mode 100644 index 0000000..eb0d605 --- /dev/null +++ b/entry/mcEntry_test.go @@ -0,0 +1,86 @@ +package entry + +import ( + "testing" + + "github.com/bradfitz/gomemcache/memcache" + "github.com/buaazp/uq/queue" + "github.com/buaazp/uq/store" + . "github.com/smartystreets/goconvey/convey" +) + +var mc *memcache.Client + +func init() { + mc = memcache.New("localhost:8802") +} + +func TestNewMcEntry(t *testing.T) { + Convey("Test New Mc Entry", t, func() { + var err error + storage, err = store.NewMemStore() + So(err, ShouldBeNil) + So(storage, ShouldNotBeNil) + messageQueue, err = queue.NewUnitedQueue(storage, "127.0.0.1", 8802, nil, "uq") + So(err, ShouldBeNil) + So(messageQueue, ShouldNotBeNil) + + entrance, err = NewMcEntry("0.0.0.0", 8802, messageQueue) + So(err, ShouldBeNil) + So(entrance, ShouldNotBeNil) + + go func() { + entrance.ListenAndServe() + }() + }) +} + +func TestMcAdd(t *testing.T) { + Convey("Test Mc Add Api", t, func() { + err := mc.Add(&memcache.Item{ + Key: "foo", + Value: []byte{}, + }) + So(err, ShouldBeNil) + + err = mc.Add(&memcache.Item{ + Key: "foo/x", + Value: []byte("10s"), + }) + So(err, ShouldBeNil) + }) +} + +func TestMcPush(t *testing.T) { + Convey("Test Mc Push Api", t, func() { + err := mc.Set(&memcache.Item{ + Key: "foo", + Value: []byte("1"), + }) + So(err, ShouldBeNil) + }) +} + +func TestMcPop(t *testing.T) { + Convey("Test Mc Pop Api", t, func() { + it, err := mc.Get("foo/x") + So(err, ShouldBeNil) + v := string(it.Value) + So(v, ShouldEqual, "1") + }) +} + +func TestMcConfirm(t *testing.T) { + Convey("Test Mc Confirm Api", t, func() { + err := mc.Delete("foo/x/0") + So(err, ShouldBeNil) + }) +} + +func TestCloseMcEntry(t *testing.T) { + Convey("Test Close Mc Entry", t, func() { + entrance.Stop() + messageQueue = nil + storage = nil + }) +} diff --git a/entry/redisEntry_test.go b/entry/redisEntry_test.go new file mode 100644 index 0000000..e16bf21 --- /dev/null +++ b/entry/redisEntry_test.go @@ -0,0 +1,81 @@ +package entry + +import ( + "testing" + "time" + + "github.com/buaazp/uq/queue" + "github.com/buaazp/uq/store" + "github.com/garyburd/redigo/redis" + . "github.com/smartystreets/goconvey/convey" +) + +var conn redis.Conn + +func TestNewRedisEntry(t *testing.T) { + Convey("Test New Redis Entry", t, func() { + var err error + storage, err = store.NewMemStore() + So(err, ShouldBeNil) + So(storage, ShouldNotBeNil) + messageQueue, err = queue.NewUnitedQueue(storage, "127.0.0.1", 8803, nil, "uq") + So(err, ShouldBeNil) + So(messageQueue, ShouldNotBeNil) + + entrance, err = NewRedisEntry("0.0.0.0", 8803, messageQueue) + So(err, ShouldBeNil) + So(entrance, ShouldNotBeNil) + + go func() { + entrance.ListenAndServe() + }() + }) +} + +func TestRedisAdd(t *testing.T) { + Convey("Test Redis Add Api", t, func() { + conn, _ = redis.DialTimeout("tcp", "127.0.0.1:8803", 0, 1*time.Second, 1*time.Second) + So(conn, ShouldNotBeNil) + + _, err := conn.Do("QADD", "foo") + So(err, ShouldBeNil) + + _, err = conn.Do("QADD", "foo/x", "10s") + So(err, ShouldBeNil) + }) +} + +func TestRedisPush(t *testing.T) { + Convey("Test Redis Push Api", t, func() { + _, err := conn.Do("QPUSH", "foo", "1") + So(err, ShouldBeNil) + }) +} + +func TestRedisPop(t *testing.T) { + Convey("Test Redis Pop Api", t, func() { + rpl, err := redis.Values(conn.Do("QPOP", "foo/x")) + So(err, ShouldBeNil) + v, err := redis.String(rpl[0], err) + So(err, ShouldBeNil) + So(v, ShouldEqual, "1") + id, err := redis.String(rpl[1], err) + So(err, ShouldBeNil) + So(id, ShouldEqual, "foo/x/0") + }) +} + +func TestRedisConfirm(t *testing.T) { + Convey("Test Redis Confirm Api", t, func() { + _, err := conn.Do("QDEL", "foo/x/0") + So(err, ShouldBeNil) + }) +} + +func TestCloseRedisEntry(t *testing.T) { + Convey("Test Close Redis Entry", t, func() { + entrance.Stop() + messageQueue = nil + storage = nil + }) +} diff --git a/uq_test.go b/uq_test.go new file mode 100644 index 0000000..364a1bb --- /dev/null +++ b/uq_test.go @@ -0,0 +1,18 @@ +package main + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestArgs(t *testing.T) { + Convey("Test UQ CMD Args", t, func() { + So(checkArgs(), ShouldEqual, true) + db = "mysql" + So(checkArgs(), ShouldEqual, false) + db = "memdb" + protocol = "http2" + So(checkArgs(), ShouldEqual, false) + }) +} diff --git a/utils/error.go b/utils/error.go index c32e853..6b5236c 100644 --- a/utils/error.go +++ b/utils/error.go @@ -62,11 +62,6 @@ func (e Error) Error() string { return ItoaQuick(e.ErrorCode) + " " + e.Message + " (" + e.Cause + ")" } -func (e Error) toJsonString() string { - b, _ := json.Marshal(e) - return string(b) -} - func (e Error) statusCode() int { status, ok := errorStatus[e.ErrorCode] if !ok { @@ -75,6 +70,11 @@ func (e Error) statusCode() int { return status } +func (e Error) toJsonString() string { + b, _ := json.Marshal(e) + return string(b) +} + func (e Error) WriteTo(w http.ResponseWriter) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(e.statusCode()) diff --git a/utils/error_test.go b/utils/error_test.go new file mode 100644 index 0000000..c3e36a5 --- /dev/null +++ b/utils/error_test.go @@ -0,0 +1,21 @@ +package utils + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestError(t *testing.T) { + Convey("Test Error", t, func() { + err := NewError( + ErrInternalError, + `This is a test error`, + ) + So(err.statusCode(), ShouldEqual, 500) + So(err.Error(), ShouldEqual, "500 Internal Error (This is a test error)") + So(err.toJsonString(), ShouldEqual, + `{"errorCode":500,"message":"Internal Error","cause":"This is a test error"}`, + ) + }) +}