mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
0b0406f41a
* feat: 解决goreportcard的警报 ps: warning: if block ends with a return statement, so drop this else and outdent its block (golint) * feat: 优化golint警告,将processFieldPrimitiveWithJsonNumber 改成 processFieldPrimitiveWithJSONNumber unmarshaler.go:248:23: method processFieldPrimitiveWithJsonNumber should be processFieldPrimitiveWithJSONNumber * feat: 添加 WithCanonicalKeyFunc 注释 * feat: 添加DisableStat的注释 * feat: 添加 RegisterGoctlHome 注释 * feat: 添加 PostgreSqlJoin 注释 * feat: 解决goline警告should not use basic type string as key in context.WithValue问题 * feat: 解决golint警告信息: should not use basic type string as key in context.WithValue * fix: 定义自定义字段类型,导致go test fail 问题 * update: 恢复原有测试用例 * fix golint warning
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package contextx
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestContextCancel(t *testing.T) {
|
|
type key string
|
|
var nameKey key = "name"
|
|
c := context.WithValue(context.Background(), nameKey, "value")
|
|
c1, cancel := context.WithCancel(c)
|
|
o := ValueOnlyFrom(c1)
|
|
c2, cancel2 := context.WithCancel(o)
|
|
defer cancel2()
|
|
contexts := []context.Context{c1, c2}
|
|
|
|
for _, c := range contexts {
|
|
assert.NotNil(t, c.Done())
|
|
assert.Nil(t, c.Err())
|
|
|
|
select {
|
|
case x := <-c.Done():
|
|
t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
|
|
default:
|
|
}
|
|
}
|
|
|
|
cancel()
|
|
<-c1.Done()
|
|
|
|
assert.Nil(t, o.Err())
|
|
assert.Equal(t, context.Canceled, c1.Err())
|
|
assert.NotEqual(t, context.Canceled, c2.Err())
|
|
}
|
|
|
|
func TestContextDeadline(t *testing.T) {
|
|
c, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Millisecond))
|
|
cancel()
|
|
o := ValueOnlyFrom(c)
|
|
select {
|
|
case <-time.After(100 * time.Millisecond):
|
|
case <-o.Done():
|
|
t.Fatal("ValueOnlyContext: context should not have timed out")
|
|
}
|
|
|
|
c, cancel = context.WithDeadline(context.Background(), time.Now().Add(10*time.Millisecond))
|
|
cancel()
|
|
o = ValueOnlyFrom(c)
|
|
c, cancel = context.WithDeadline(o, time.Now().Add(20*time.Millisecond))
|
|
defer cancel()
|
|
select {
|
|
case <-time.After(100 * time.Millisecond):
|
|
t.Fatal("ValueOnlyContext+Deadline: context should have timed out")
|
|
case <-c.Done():
|
|
}
|
|
}
|