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,13 +61,13 @@ func tracetestExample(t *tracetest.T) { ...@@ -65,13 +61,13 @@ func tracetestExample(t *tracetest.T) {
t.RxEvent(eventHello(who)) t.RxEvent(eventHello(who))
}) })
tracing.Unlock() tracing.Unlock()
defer pg.Done() return pg
}
// tell tracetest to which stream an event should go. // routeEvent tells to which stream an event should go.
t.SetEventRouter(func(event interface{}) (stream string) { // Here, in example, we use the convention that who comes as "<threadID>·..."
// it can be only eventHi and eventHello. // and we route the event to stream that corresponds to threadID.
// in this test the convention is that who comes as <threadID>·... func routeEvent(event interface{}) (stream string) {
// we used threadID as stream.
who := "" who := ""
switch ev := event.(type) { switch ev := event.(type) {
default: default:
...@@ -88,7 +84,20 @@ func tracetestExample(t *tracetest.T) { ...@@ -88,7 +84,20 @@ func tracetestExample(t *tracetest.T) {
} }
return strings.ToLower(who[:i]) 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(routeEvent)
// 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