| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package local_ctx
- import (
- "context"
- "testing"
- )
- type tester struct {
- t *testing.T
- lCtx *LocalCtx
- }
- func TestLocalCtx(t *testing.T) {
- sf := &tester{
- t: t,
- }
- sf.new()
- sf.set()
- sf.get()
- sf.del()
- sf.done()
- }
- // Ожидает отмены контекста.
- func (sf *tester) done() {
- sf.t.Log("done")
- _ = sf.lCtx.Size()
- go sf.lCtx.Cancel()
- sf.lCtx.Wait()
- }
- // Удаляет несуществующий ключ из локального контекста.
- func (sf *tester) del() {
- sf.t.Log("del")
- sf.lCtx.Del("123")
- sf.lCtx.Del("count")
- }
- // Возвращает хранимое значение.
- func (sf *tester) get() {
- sf.t.Log("get")
- optRes := sf.lCtx.Get("count")
- ctxVal := optRes.Hassert("get()")
- count := ctxVal.Val().(int)
- if count == 15 {
- return
- }
- }
- // Устанавливает значение.
- func (sf *tester) set() {
- sf.t.Log("set")
- sf.lCtx.Set("count", 5, "test_val")
- sf.lCtx.Set("count", 15, "test_val1")
- }
- // Создание нового локального контекста.
- func (sf *tester) new() {
- sf.t.Log("new")
- sf.newBad1()
- sf.newGood1()
- }
- func (sf *tester) newGood1() {
- sf.t.Log("newGood1")
- ctx := context.Background()
- sf.lCtx = NewLocalCtx(ctx).(*LocalCtx)
- _ = sf.lCtx.Log()
- if lst := sf.lCtx.SortedList(); lst == nil {
- sf.t.Fatalf("newGood1(): lst==nil")
- }
- if ctx := sf.lCtx.Ctx(); ctx == nil {
- sf.t.Fatalf("newGood1(): ctx==nil")
- }
- }
- // Нет контекста ядра.
- func (sf *tester) newBad1() {
- sf.t.Log("newBad1")
- defer func() {
- if _panic := recover(); _panic == nil {
- sf.t.Fatalf("newBad1(): panic==nil")
- }
- }()
- var ctx context.Context
- sf.lCtx = NewLocalCtx(ctx).(*LocalCtx)
- }
|