Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Kirill Smelkov
neo
Commits
0255bff1
Commit
0255bff1
authored
Dec 24, 2020
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
c481b920
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
32 deletions
+41
-32
go/internal/xtracing/tracetest/example_test.go
go/internal/xtracing/tracetest/example_test.go
+41
-32
No files found.
go/internal/xtracing/tracetest/example_test.go
View file @
0255bff1
...
...
@@ -18,6 +18,8 @@
// See https://www.nexedi.com/licensing for rationale and options.
// Package tracetest_test demonstrates how to use package tracetest.
//
// It also serves as set of testcases for tracetest itself.
package
tracetest_test
//go:generate gotrace gen .
...
...
@@ -36,26 +38,20 @@ import (
//trace:event traceHi(who string)
//trace:event traceHello(who string)
func
hi
(
who
string
)
{
traceHi
(
who
)
fmt
.
Println
(
"Hi,"
,
who
)
}
func
hello
(
who
string
)
{
traceHello
(
who
)
fmt
.
Println
(
"Hello,"
,
who
)
}
// TestExample demonstrates how to use tracetest to verify concurrent system with 2 threads.
func
TestExample
(
t
*
testing
.
T
)
{
tracetest
.
Verify
(
t
,
tracetestExample
)
}
func
tracetestExample
(
t
*
tracetest
.
T
)
{
type
eventHi
string
type
eventHello
string
// setup tracing to deliver trace events to t.
// we use tracing to attach probes to hi and hello, and emit corresponding
// eventHi and eventHello to tracetest.T from there.
type
eventHi
string
type
eventHello
string
func
setupTracing
(
t
*
tracetest
.
T
)
*
tracing
.
ProbeGroup
{
pg
:=
&
tracing
.
ProbeGroup
{}
tracing
.
Lock
()
traceHi_Attach
(
pg
,
func
(
who
string
)
{
...
...
@@ -65,30 +61,43 @@ func tracetestExample(t *tracetest.T) {
t
.
RxEvent
(
eventHello
(
who
))
})
tracing
.
Unlock
()
return
pg
}
// routeEvent tells to which stream an event should go.
// Here, in example, we use the convention that who comes as "<threadID>·..."
// and we route the event to stream that corresponds to threadID.
func
routeEvent
(
event
interface
{})
(
stream
string
)
{
who
:=
""
switch
ev
:=
event
.
(
type
)
{
default
:
panic
(
fmt
.
Sprintf
(
"unexpected event type %T"
,
event
))
case
eventHi
:
who
=
string
(
ev
)
case
eventHello
:
who
=
string
(
ev
)
}
i
:=
strings
.
Index
(
who
,
"·"
)
if
i
==
-
1
{
panic
(
fmt
.
Sprintf
(
"who does not have threadID: %q"
,
who
))
}
return
strings
.
ToLower
(
who
[
:
i
])
}
// TestExample demonstrates how to use tracetest to verify concurrent system with 2 threads.
func
TestExample
(
t
*
testing
.
T
)
{
tracetest
.
Verify
(
t
,
tracetestExample
)
}
func
tracetestExample
(
t
*
tracetest
.
T
)
{
// setup tracing to deliver trace events to t.
pg
:=
setupTracing
(
t
)
defer
pg
.
Done
()
// tell tracetest to which stream an event should go.
t
.
SetEventRouter
(
func
(
event
interface
{})
(
stream
string
)
{
// it can be only eventHi and eventHello.
// in this test the convention is that who comes as <threadID>·...
// we used threadID as stream.
who
:=
""
switch
ev
:=
event
.
(
type
)
{
default
:
panic
(
fmt
.
Sprintf
(
"unexpected event type %T"
,
event
))
case
eventHi
:
who
=
string
(
ev
)
case
eventHello
:
who
=
string
(
ev
)
}
i
:=
strings
.
Index
(
who
,
"·"
)
if
i
==
-
1
{
panic
(
fmt
.
Sprintf
(
"who does not have threadID: %q"
,
who
))
}
return
strings
.
ToLower
(
who
[
:
i
])
})
t
.
SetEventRouter
(
routeEvent
)
// run the workload
var
wg
sync
.
WaitGroup
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment