| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package local_ctx
- import (
- "context"
- "testing"
- "gitp78su.ipnodns.ru/svi/kern/v4/lev0"
- )
- 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(lev0.LetTxt("123"))
- sf.lCtx.Del(lev0.LetTxt("count"))
- }
- // Возвращает хранимое значение.
- func (sf *tester) get() {
- sf.t.Log("get")
- optRes := sf.lCtx.Get(lev0.LetTxt("count"))
- ctxVal := optRes.Hassert("get()")
- count := ctxVal.Val().(lev0.Num)
- if count == 15 {
- return
- }
- }
- // Устанавливает значение.
- func (sf *tester) set() {
- sf.t.Log("set")
- sf.lCtx.Set(lev0.LetTxt("count"), 5, "test_val")
- sf.lCtx.Set(lev0.LetTxt("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)
- _ = sf.lCtx.Log()
- if lst := sf.lCtx.SortedList(); lst == nil {
- sf.t.Fatalf("newGood1(): lst==nil")
- }
- if ctx := sf.lCtx.SelfCtx(); 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)
- }
|