Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit d6129b3

Browse files
committed
Adds ITR cache subtest support
1 parent 0146678 commit d6129b3

File tree

3 files changed

+50
-34
lines changed

3 files changed

+50
-34
lines changed

instrumentation/gocheck/types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ func isTestCached(c *chk.C) bool {
295295
cachedMap := config.GetCachedTestsMap()
296296
if _, ok := cachedMap[fqn]; ok {
297297
instrumentation.Logger().Printf("Test '%v' is cached.", fqn)
298-
fmt.Print("[SCOPE CACHED] ")
299298
return true
300299
}
301300
instrumentation.Logger().Printf("Test '%v' is not cached.", fqn)

instrumentation/testing/config/testing.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ import (
66
"sync"
77
)
88

9+
type (
10+
TestDescription struct {
11+
Suite string
12+
Name string
13+
}
14+
)
15+
916
var (
10-
testsToSkip map[string]struct{}
17+
testsToSkip map[string]TestDescription
1118

1219
m sync.Mutex
1320
)
1421

1522
// Gets the map of cached tests
16-
func GetCachedTestsMap() map[string]struct{} {
23+
func GetCachedTestsMap() map[string]TestDescription {
1724
m.Lock()
1825
defer m.Unlock()
1926

@@ -22,14 +29,19 @@ func GetCachedTestsMap() map[string]struct{} {
2229
}
2330

2431
config := instrumentation.GetRemoteConfiguration()
25-
testsToSkip = map[string]struct{}{}
32+
testsToSkip = map[string]TestDescription{}
2633
if config != nil {
2734
if iCached, ok := config["cached"]; ok {
2835
cachedTests := iCached.([]interface{})
2936
for _, item := range cachedTests {
3037
testItem := item.(map[string]interface{})
31-
testFqn := fmt.Sprintf("%v.%v", testItem["test_suite"], testItem["test_name"])
32-
testsToSkip[testFqn] = struct{}{}
38+
suite := fmt.Sprint(testItem["test_suite"])
39+
name := fmt.Sprint(testItem["test_name"])
40+
testFqn := fmt.Sprintf("%s.%s", suite, name)
41+
testsToSkip[testFqn] = TestDescription{
42+
Suite: suite,
43+
Name: name,
44+
}
3345
}
3446
}
3547
}

instrumentation/testing/testing.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,30 @@ func StartTest(t *testing.T, opts ...Option) *Test {
6363
func StartTestFromCaller(t *testing.T, pc uintptr, opts ...Option) *Test {
6464

6565
// check if the test is cached
66-
if isTestCached(t, pc) {
67-
68-
test := &Test{t: t, ctx: context.Background()}
69-
for _, opt := range opts {
70-
opt(test)
71-
}
72-
73-
// Extracting the testing func name (by removing any possible sub-test suffix `{test_func}/{sub_test}`)
74-
// to search the func source code bounds and to calculate the package name.
75-
fullTestName := runner.GetOriginalTestName(t.Name())
76-
pName, _ := instrumentation.GetPackageAndName(pc)
77-
78-
testTags := opentracing.Tags{
79-
"span.kind": "test",
80-
"test.name": fullTestName,
81-
"test.suite": pName,
82-
"test.framework": "testing",
83-
"test.language": "go",
66+
if ok, testsDescription := isTestCached(t, pc); ok {
67+
ctx := context.Background()
68+
tracer := instrumentation.Tracer()
69+
for _, desc := range testsDescription {
70+
startTime := time.Now()
71+
options := []opentracing.StartSpanOption{
72+
opentracing.Tags{
73+
"span.kind": "test",
74+
"test.name": desc.Name,
75+
"test.suite": desc.Suite,
76+
"test.framework": "testing",
77+
"test.language": "go",
78+
},
79+
opentracing.StartTime(startTime),
80+
}
81+
span, _ := opentracing.StartSpanFromContextWithTracer(ctx, tracer, desc.Name, options...)
82+
span.SetBaggageItem("trace.kind", "test")
83+
span.SetTag("test.status", tags.TestStatus_CACHE)
84+
span.FinishWithOptions(opentracing.FinishOptions{
85+
FinishTime: startTime,
86+
})
8487
}
85-
span, _ := opentracing.StartSpanFromContextWithTracer(test.ctx, instrumentation.Tracer(), fullTestName, testTags)
86-
span.SetBaggageItem("trace.kind", "test")
87-
span.SetTag("test.status", tags.TestStatus_CACHE)
88-
span.Finish()
8988
t.SkipNow()
90-
return test
89+
return nil
9190

9291
} else {
9392

@@ -320,16 +319,22 @@ func addAutoInstrumentedTest(t *testing.T) {
320319
}
321320

322321
// Get if the test is cached
323-
func isTestCached(t *testing.T, pc uintptr) bool {
324-
pkgName, testName := instrumentation.GetPackageAndName(pc)
322+
func isTestCached(t *testing.T, pc uintptr) (bool, []config.TestDescription) {
323+
pkgName, _ := instrumentation.GetPackageAndName(pc)
324+
testName := runner.GetOriginalTestName(t.Name())
325325
fqn := fmt.Sprintf("%s.%s", pkgName, testName)
326326
cachedMap := config.GetCachedTestsMap()
327327
if _, ok := cachedMap[fqn]; ok {
328328
instrumentation.Logger().Printf("Test '%v' is cached.", fqn)
329-
fmt.Print("[SCOPE CACHED] ")
329+
var tests []config.TestDescription
330+
for _, v := range cachedMap {
331+
if v.Suite == pkgName && strings.HasPrefix(v.Name, testName) {
332+
tests = append(tests, v)
333+
}
334+
}
330335
reflection.SkipAndFinishTest(t)
331-
return true
336+
return true, tests
332337
}
333338
instrumentation.Logger().Printf("Test '%v' is not cached.", fqn)
334-
return false
339+
return false, nil
335340
}

0 commit comments

Comments
 (0)