1
0
镜像自地址 https://github.com/tuna/tunasync.git 已同步 2025-12-06 14:36:47 +00:00

feature(worker): context object to store runtime configurations

这个提交包含在:
bigeagle
2016-04-21 20:45:06 +08:00
父节点 f0a0552e50
当前提交 f95a0f9a6f
共有 3 个文件被更改,包括 138 次插入0 次删除

64
worker/context_test.go 普通文件
查看文件

@@ -0,0 +1,64 @@
package worker
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestContext(t *testing.T) {
Convey("Context should work", t, func() {
ctx := NewContext()
So(ctx, ShouldNotBeNil)
So(ctx.parent, ShouldBeNil)
ctx.Set("logdir1", "logdir_value_1")
ctx.Set("logdir2", "logdir_value_2")
logdir, ok := ctx.Get("logdir1")
So(ok, ShouldBeTrue)
So(logdir, ShouldEqual, "logdir_value_1")
Convey("When entering a new context", func() {
ctx = ctx.Enter()
logdir, ok = ctx.Get("logdir1")
So(ok, ShouldBeTrue)
So(logdir, ShouldEqual, "logdir_value_1")
ctx.Set("logdir1", "new_value_1")
logdir, ok = ctx.Get("logdir1")
So(ok, ShouldBeTrue)
So(logdir, ShouldEqual, "new_value_1")
logdir, ok = ctx.Get("logdir2")
So(ok, ShouldBeTrue)
So(logdir, ShouldEqual, "logdir_value_2")
Convey("When accesing invalid key", func() {
logdir, ok = ctx.Get("invalid_key")
So(ok, ShouldBeFalse)
So(logdir, ShouldBeNil)
})
Convey("When exiting the new context", func() {
ctx, err := ctx.Exit()
So(err, ShouldBeNil)
logdir, ok = ctx.Get("logdir1")
So(ok, ShouldBeTrue)
So(logdir, ShouldEqual, "logdir_value_1")
logdir, ok = ctx.Get("logdir2")
So(ok, ShouldBeTrue)
So(logdir, ShouldEqual, "logdir_value_2")
Convey("When exiting from top bottom context", func() {
ctx, err := ctx.Exit()
So(err, ShouldNotBeNil)
So(ctx, ShouldBeNil)
})
})
})
})
}