From 7db97fc31db5facc2c8dcfdea0dd716cee28e632 Mon Sep 17 00:00:00 2001 From: Xing Xing Date: Tue, 24 Dec 2013 16:35:17 +0800 Subject: [PATCH] package's test-case added --- worker/common.go | 1 + worker/inpack_test.go | 62 +++++++++++++++++++++++++++++++ worker/outpack.go | 6 +-- worker/outpack_test.go | 83 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 worker/inpack_test.go create mode 100644 worker/outpack_test.go diff --git a/worker/common.go b/worker/common.go index 8fbcf10..991bff4 100644 --- a/worker/common.go +++ b/worker/common.go @@ -41,6 +41,7 @@ const ( STATUS_RES = 20 SET_CLIENT_ID = 22 CAN_DO_TIMEOUT = 23 + ALL_YOURS = 24 WORK_EXCEPTION = 25 WORK_DATA = 28 WORK_WARNING = 29 diff --git a/worker/inpack_test.go b/worker/inpack_test.go new file mode 100644 index 0000000..75b9ba5 --- /dev/null +++ b/worker/inpack_test.go @@ -0,0 +1,62 @@ +package worker + +import ( + "bytes" + "testing" +) + +var ( + inpackcases = map[uint32]map[string]string{ + NOOP: map[string]string{ + "src": "\x00RES\x00\x00\x00\x06\x00\x00\x00\x00", + }, + NO_JOB: map[string]string{ + "src": "\x00RES\x00\x00\x00\x0a\x00\x00\x00\x00", + }, + JOB_ASSIGN: map[string]string{ + "src": "\x00RES\x00\x00\x00\x0b\x00\x00\x00\x07a\x00b\x00xyz", + "handle": "a", + "fn": "b", + "data": "xyz", + }, + JOB_ASSIGN_UNIQ: map[string]string{ + "src": "\x00RES\x00\x00\x00\x1F\x00\x00\x00\x09a\x00b\x00c\x00xyz", + "handle": "a", + "fn": "b", + "uid": "c", + "data": "xyz", + }, + } +) + +func TestInPack(t *testing.T) { + for k, v := range inpackcases { + inpack, _, err := decodeInPack([]byte(v["src"])) + if err != nil { + t.Error(err) + } + if inpack.dataType != k { + t.Errorf("DataType: %d expected, %d got.", k, inpack.dataType) + } + if handle, ok := v["handle"]; ok { + if inpack.handle != handle { + t.Errorf("Handle: %s expected, %s got.", handle, inpack.handle) + } + } + if fn, ok := v["fn"]; ok { + if inpack.fn != fn { + t.Errorf("FuncName: %s expected, %s got.", fn, inpack.fn) + } + } + if uid, ok := v["uid"]; ok { + if inpack.uniqueId != uid { + t.Errorf("UID: %s expected, %s got.", uid, inpack.uniqueId) + } + } + if data, ok := v["data"]; ok { + if bytes.Compare([]byte(data), inpack.data) != 0 { + t.Errorf("UID: %v expected, %v got.", data, inpack.data) + } + } + } +} diff --git a/worker/outpack.go b/worker/outpack.go index 45e7c58..9464b8c 100644 --- a/worker/outpack.go +++ b/worker/outpack.go @@ -12,9 +12,9 @@ import ( // Worker side job type outPack struct { - dataType uint32 - data []byte - handle, uniqueId, fn string + dataType uint32 + data []byte + handle string } func getOutPack() (outpack *outPack) { diff --git a/worker/outpack_test.go b/worker/outpack_test.go new file mode 100644 index 0000000..dca1d8e --- /dev/null +++ b/worker/outpack_test.go @@ -0,0 +1,83 @@ +package worker + +import ( + "bytes" + "testing" +) + +var ( + outpackcases = map[uint32]map[string]string{ + CAN_DO: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x01\x00\x00\x00\x01a", + "data": "a", + }, + CAN_DO_TIMEOUT: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x17\x00\x00\x00\x06a\x00\x00\x00\x00\x01", + "data": "a\x00\x00\x00\x00\x01", + }, + CANT_DO: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x02\x00\x00\x00\x01a", + "data": "a", + }, + RESET_ABILITIES: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x03\x00\x00\x00\x00", + }, + PRE_SLEEP: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x04\x00\x00\x00\x00", + }, + GRAB_JOB: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x09\x00\x00\x00\x00", + }, + GRAB_JOB_UNIQ: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x1E\x00\x00\x00\x00", + }, + WORK_DATA: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x1C\x00\x00\x00\x03a\x00b", + "data": "a\x00b", + }, + WORK_WARNING: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x1D\x00\x00\x00\x03a\x00b", + "data": "a\x00b", + }, + WORK_STATUS: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x0C\x00\x00\x00\x08a\x0050\x00100", + "data": "a\x0050\x00100", + }, + WORK_COMPLETE: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x0D\x00\x00\x00\x03a\x00b", + "data": "a\x00b", + }, + WORK_FAIL: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x0E\x00\x00\x00\x01a", + "handle": "a", + }, + WORK_EXCEPTION: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x19\x00\x00\x00\x03a\x00b", + "data": "a\x00b", + }, + SET_CLIENT_ID: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x16\x00\x00\x00\x01a", + "data": "a", + }, + ALL_YOURS: map[string]string{ + "src": "\x00REQ\x00\x00\x00\x18\x00\x00\x00\x00", + }, + } +) + +func TestOutPack(t *testing.T) { + for k, v := range outpackcases { + outpack := getOutPack() + outpack.dataType = k + if handle, ok := v["handle"]; ok { + outpack.handle = handle + } + if data, ok := v["data"]; ok { + outpack.data = []byte(data) + } + data := outpack.Encode() + if bytes.Compare([]byte(v["src"]), data) != 0 { + t.Errorf("%d: %X expected, %X got.", k, v["src"], data) + } + } +}