From 099eb863400c4a3dc2823fd3dbd8db1ca75da26f Mon Sep 17 00:00:00 2001 From: hantmac Date: Sun, 18 Aug 2019 14:20:37 +0800 Subject: [PATCH] Add Unit Test for backend/utils/chan.go --- backend/utils/chan_test.go | 76 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 backend/utils/chan_test.go diff --git a/backend/utils/chan_test.go b/backend/utils/chan_test.go new file mode 100644 index 00000000..7b5f1bba --- /dev/null +++ b/backend/utils/chan_test.go @@ -0,0 +1,76 @@ +package utils + +import ( + . "github.com/smartystreets/goconvey/convey" + "testing" +) + +func TestNewChanMap(t *testing.T) { + mapTest := make(map[string]chan string) + chanTest := make(chan string) + test := "test" + + Convey("Call NewChanMap to generate ChanMap", t, func() { + mapTest[test] = chanTest + chanMapTest := ChanMap{mapTest} + chanMap := NewChanMap() + chanMap.m[test] = chanTest + + Convey(test, func() { + So(chanMap, ShouldResemble, &chanMapTest) + }) + + }) +} + +func TestChan(t *testing.T) { + mapTest := make(map[string]chan string) + chanTest := make(chan string) + mapTest["test"] = chanTest + chanMapTest := ChanMap{mapTest} + + Convey("Test Chan use exist key", t, func() { + ch1 := chanMapTest.Chan( + "test") + Convey("ch1 should equal chanTest", func() { + So(ch1, ShouldEqual, chanTest) + }) + + }) + Convey("Test Chan use no-exist key", t, func() { + ch2 := chanMapTest.Chan("test2") + Convey("ch2 should equal chanMapTest.m[test2]", func() { + + So(chanMapTest.m["test2"], ShouldEqual, ch2) + }) + Convey("Cap of chanMapTest.m[test2] should equal 10", func() { + So(10, ShouldEqual, cap(chanMapTest.m["test2"])) + }) + }) +} + +func TestChanBlocked(t *testing.T) { + mapTest := make(map[string]chan string) + chanTest := make(chan string) + mapTest["test"] = chanTest + chanMapTest := ChanMap{mapTest} + + Convey("Test Chan use exist key", t, func() { + ch1 := chanMapTest.ChanBlocked( + "test") + Convey("ch1 should equal chanTest", func() { + So(ch1, ShouldEqual, chanTest) + }) + + }) + Convey("Test Chan use no-exist key", t, func() { + ch2 := chanMapTest.ChanBlocked("test2") + Convey("ch2 should equal chanMapTest.m[test2]", func() { + + So(chanMapTest.m["test2"], ShouldEqual, ch2) + }) + Convey("Cap of chanMapTest.m[test2] should equal 10", func() { + So(0, ShouldEqual, cap(chanMapTest.m["test2"])) + }) + }) +}