Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
856a517
commit 9efce4e
Showing
8 changed files
with
602 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package floc | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestNewContext(t *testing.T) { | ||
ctx := NewContext() | ||
ctx.Release() | ||
} | ||
|
||
func TestFlowContext_Ctx(t *testing.T) { | ||
const key = "KEY" | ||
const value = "VALUE" | ||
|
||
ctx := NewContext() | ||
defer ctx.Release() | ||
|
||
innerCtx := ctx.Ctx() | ||
ctx.UpdateCtx(context.WithValue(innerCtx, key, value)) | ||
|
||
v := ctx.Ctx().Value(key) | ||
if v == nil { | ||
t.Fatalf("%s expects value to be not nil", t.Name()) | ||
} | ||
|
||
if s, ok := v.(string); !ok { | ||
t.Fatalf("%s expects value to be of type string", t.Name()) | ||
} else if s != value { | ||
t.Fatalf("%s expects value to be %s but has %s", t.Name(), value, s) | ||
} | ||
} | ||
|
||
func TestFlowContext_Value(t *testing.T) { | ||
const max = 1000 | ||
|
||
ctx := NewContext() | ||
defer ctx.Release() | ||
|
||
var wg sync.WaitGroup | ||
var n int | ||
for n = 0; n < max; n++ { | ||
wg.Add(1) | ||
|
||
go func(n int) { | ||
ctx.AddValue(n, n) | ||
wg.Done() | ||
}(n) | ||
} | ||
|
||
wg.Wait() | ||
|
||
for n = 0; n < max; n++ { | ||
v := ctx.Value(n) | ||
if v == nil { | ||
t.Fatalf("%s expects value to be not nil", t.Name()) | ||
} | ||
|
||
if d, ok := v.(int); !ok { | ||
t.Fatalf("%s expects value %d to be of type int", t.Name(), n) | ||
} else if d != n { | ||
t.Fatalf("%s expects value %d to be %d but has %d", t.Name(), n, n, d) | ||
} | ||
} | ||
} | ||
|
||
func TestFlowContext_Done(t *testing.T) { | ||
ctx := NewContext() | ||
defer ctx.Release() | ||
|
||
cancelCtx, cancel := context.WithCancel(ctx.Ctx()) | ||
ctx.UpdateCtx(cancelCtx) | ||
|
||
var wg sync.WaitGroup | ||
for i := 0; i < 10; i++ { | ||
wg.Add(1) | ||
|
||
go func() { | ||
<-ctx.Done() | ||
wg.Done() | ||
}() | ||
} | ||
|
||
cancel() | ||
|
||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.