Commit 0255bff1 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent c481b920
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment