-
Notifications
You must be signed in to change notification settings - Fork 15
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
Showing
1 changed file
with
106 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package gotaskflow | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestProfilerStartStop(t *testing.T) { | ||
profiler := NewTracer() | ||
profiler.Start() | ||
time.Sleep(10 * time.Millisecond) | ||
profiler.Stop() | ||
|
||
if profiler.start.After(profiler.end) { | ||
t.Errorf("expected start time before end time, got start: %v, end: %v", profiler.start, profiler.end) | ||
} | ||
|
||
if profiler.start.IsZero() || profiler.end.IsZero() { | ||
t.Errorf("expected start and end times to be set, got start: %v, end: %v", profiler.start, profiler.end) | ||
} | ||
} | ||
|
||
func TestProfilerAddSpan(t *testing.T) { | ||
profiler := NewTracer() | ||
span := &span{ | ||
extra: attr{ | ||
typ: NodeStatic, | ||
success: true, | ||
name: "test-span", | ||
}, | ||
begin: time.Now(), | ||
end: time.Now().Add(5 * time.Millisecond), | ||
} | ||
profiler.AddSpan(span) | ||
|
||
if len(profiler.spans) != 1 { | ||
t.Errorf("expected 1 span, got %d", len(profiler.spans)) | ||
} | ||
|
||
if profiler.spans[0] != span { | ||
t.Errorf("expected span to be added correctly, got %v", profiler.spans[0]) | ||
} | ||
} | ||
|
||
func TestSpanString(t *testing.T) { | ||
span := &span{ | ||
extra: attr{ | ||
typ: NodeStatic, | ||
success: true, | ||
name: "test-span", | ||
}, | ||
begin: time.Now(), | ||
end: time.Now().Add(10 * time.Millisecond), | ||
} | ||
|
||
expected := "NodeStatic,test-span,cost 10000µs" | ||
actual := span.String() | ||
|
||
if actual != expected { | ||
t.Errorf("expected %s, got %s", expected, actual) | ||
} | ||
} | ||
|
||
func TestProfilerDraw(t *testing.T) { | ||
profiler := NewTracer() | ||
parentSpan := &span{ | ||
extra: attr{ | ||
typ: NodeStatic, | ||
success: true, | ||
name: "parent", | ||
}, | ||
begin: time.Now(), | ||
end: time.Now().Add(10 * time.Millisecond), | ||
} | ||
|
||
childSpan := &span{ | ||
extra: attr{ | ||
typ: NodeStatic, | ||
success: true, | ||
name: "child", | ||
}, | ||
begin: time.Now(), | ||
end: time.Now().Add(5 * time.Millisecond), | ||
parent: parentSpan, | ||
} | ||
|
||
profiler.AddSpan(parentSpan) | ||
profiler.AddSpan(childSpan) | ||
|
||
var buf bytes.Buffer | ||
err := profiler.draw(&buf) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
|
||
output := buf.String() | ||
if len(output) == 0 { | ||
t.Errorf("expected output, got empty string") | ||
} | ||
|
||
expectedOutput := "NodeStatic,parent,cost 10000µs;NodeStatic,child,cost 5000µs 5000\n" | ||
if output != expectedOutput { | ||
t.Errorf("expected output: %s, got: %s", expectedOutput, output) | ||
} | ||
} |