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

.

parent c481b920
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
// See https://www.nexedi.com/licensing for rationale and options. // See https://www.nexedi.com/licensing for rationale and options.
// Package tracetest_test demonstrates how to use package tracetest. // Package tracetest_test demonstrates how to use package tracetest.
//
// It also serves as set of testcases for tracetest itself.
package tracetest_test package tracetest_test
//go:generate gotrace gen . //go:generate gotrace gen .
...@@ -36,26 +38,20 @@ import ( ...@@ -36,26 +38,20 @@ import (
//trace:event traceHi(who string) //trace:event traceHi(who string)
//trace:event traceHello(who string) //trace:event traceHello(who string)
func hi(who string) { func hi(who string) {
traceHi(who) traceHi(who)
fmt.Println("Hi,", who) fmt.Println("Hi,", who)
} }
func hello(who string) { func hello(who string) {
traceHello(who) traceHello(who)
fmt.Println("Hello,", who) fmt.Println("Hello,", who)
} }
// TestExample demonstrates how to use tracetest to verify concurrent system with 2 threads. // we use tracing to attach probes to hi and hello, and emit corresponding
func TestExample(t *testing.T) { // eventHi and eventHello to tracetest.T from there.
tracetest.Verify(t, tracetestExample) type eventHi string
} type eventHello string
func tracetestExample(t *tracetest.T) { func setupTracing(t *tracetest.T) *tracing.ProbeGroup {
type eventHi string
type eventHello string
// setup tracing to deliver trace events to t.
pg := &tracing.ProbeGroup{} pg := &tracing.ProbeGroup{}
tracing.Lock() tracing.Lock()
traceHi_Attach(pg, func(who string) { traceHi_Attach(pg, func(who string) {
...@@ -65,30 +61,43 @@ func tracetestExample(t *tracetest.T) { ...@@ -65,30 +61,43 @@ func tracetestExample(t *tracetest.T) {
t.RxEvent(eventHello(who)) t.RxEvent(eventHello(who))
}) })
tracing.Unlock() 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() defer pg.Done()
// tell tracetest to which stream an event should go. // tell tracetest to which stream an event should go.
t.SetEventRouter(func(event interface{}) (stream string) { t.SetEventRouter(routeEvent)
// 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])
})
// run the workload // run the workload
var wg sync.WaitGroup 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