Skip to content

Commit 42013d6

Browse files
committedMay 21, 2024·
Fix table entry context edge case
1 parent 9e234ea commit 42013d6

File tree

3 files changed

+92
-62
lines changed

3 files changed

+92
-62
lines changed
 

‎internal/internal_integration/interrupt_and_timeout_test.go

+85-60
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internal_integration_test
22

33
import (
44
"context"
5+
"strconv"
56
"sync"
67
"time"
78

@@ -1013,71 +1014,95 @@ var _ = Describe("Interrupts and Timeouts", func() {
10131014
})
10141015

10151016
Describe("passing contexts to TableEntries", func() {
1016-
var times *TimeMap
1017-
BeforeEach(func() {
1018-
times = NewTimeMap()
1017+
Describe("the happy path", func() {
1018+
var times *TimeMap
1019+
BeforeEach(func() {
1020+
times = NewTimeMap()
10191021

1020-
success, _ := RunFixture(CurrentSpecReport().LeafNodeText, func() {
1021-
Context("container", func() {
1022-
DescribeTable("timeout table",
1023-
func(c SpecContext, d context.Context, key string) {
1024-
key = d.Value("key").(string) + key
1025-
rt.Run(CurrentSpecReport().LeafNodeText)
1026-
t := time.Now()
1027-
<-c.Done()
1028-
times.Set(key, time.Since(t))
1029-
},
1030-
func(d context.Context, key string) string {
1031-
key = d.Value("key").(string) + key
1032-
return key
1033-
},
1034-
Entry(nil, context.WithValue(context.Background(), "key", "entry-"), "1", NodeTimeout(time.Millisecond)*100),
1035-
Entry(nil, context.WithValue(context.Background(), "key", "entry-"), "2", SpecTimeout(time.Millisecond)*150),
1036-
)
1037-
1038-
DescribeTable("timeout table",
1039-
func(c context.Context, key string) {
1040-
rt.Run(CurrentSpecReport().LeafNodeText)
1041-
t := time.Now()
1042-
<-c.Done()
1043-
times.Set(key, time.Since(t))
1044-
},
1045-
func(key string) string {
1046-
return key
1047-
},
1048-
Entry(nil, "entry-3", NodeTimeout(time.Millisecond)*100),
1049-
Entry(nil, "entry-4", SpecTimeout(time.Millisecond)*150),
1050-
)
1051-
1052-
DescribeTable("timeout table",
1053-
func(c context.Context, key string) {
1054-
key = c.Value("key").(string) + key
1055-
rt.Run(CurrentSpecReport().LeafNodeText + "-" + key)
1056-
},
1057-
func(d context.Context, key string) string {
1058-
key = d.Value("key").(string) + key
1059-
return key
1060-
},
1061-
Entry(nil, context.WithValue(context.Background(), "key", "entry-"), "5"),
1062-
Entry(nil, context.WithValue(context.Background(), "key", "entry-"), "6"),
1063-
)
1022+
success, _ := RunFixture(CurrentSpecReport().LeafNodeText, func() {
1023+
Context("container", func() {
1024+
DescribeTable("timeout table",
1025+
func(c SpecContext, d context.Context, key string) {
1026+
key = d.Value("key").(string) + key
1027+
rt.Run(CurrentSpecReport().LeafNodeText)
1028+
t := time.Now()
1029+
<-c.Done()
1030+
times.Set(key, time.Since(t))
1031+
},
1032+
func(d context.Context, key string) string {
1033+
key = d.Value("key").(string) + key
1034+
return key
1035+
},
1036+
Entry(nil, context.WithValue(context.Background(), "key", "entry-"), "1", NodeTimeout(time.Millisecond)*100),
1037+
Entry(nil, context.WithValue(context.Background(), "key", "entry-"), "2", SpecTimeout(time.Millisecond)*150),
1038+
)
1039+
1040+
DescribeTable("timeout table",
1041+
func(c context.Context, key string) {
1042+
rt.Run(CurrentSpecReport().LeafNodeText)
1043+
t := time.Now()
1044+
<-c.Done()
1045+
times.Set(key, time.Since(t))
1046+
},
1047+
func(key string) string {
1048+
return key
1049+
},
1050+
Entry(nil, "entry-3", NodeTimeout(time.Millisecond)*100),
1051+
Entry(nil, "entry-4", SpecTimeout(time.Millisecond)*150),
1052+
)
1053+
1054+
DescribeTable("timeout table",
1055+
func(c context.Context, key string) {
1056+
key = c.Value("key").(string) + key
1057+
rt.Run(CurrentSpecReport().LeafNodeText + "-" + key)
1058+
},
1059+
func(d context.Context, key string) string {
1060+
key = d.Value("key").(string) + key
1061+
return key
1062+
},
1063+
Entry(nil, context.WithValue(context.Background(), "key", "entry-"), "5"),
1064+
Entry(nil, context.WithValue(context.Background(), "key", "entry-"), "6"),
1065+
)
1066+
})
10641067
})
1068+
Ω(success).Should(Equal(false))
1069+
})
1070+
1071+
It("should work", func() {
1072+
Ω(rt).Should(HaveTracked("entry-1", "entry-2", "entry-3", "entry-4", "entry-5-entry-5", "entry-6-entry-6"))
1073+
Ω(reporter.Did.Find("entry-1")).Should(HaveTimedOut())
1074+
Ω(reporter.Did.Find("entry-2")).Should(HaveTimedOut())
1075+
Ω(reporter.Did.Find("entry-3")).Should(HaveTimedOut())
1076+
Ω(reporter.Did.Find("entry-4")).Should(HaveTimedOut())
1077+
Ω(reporter.Did.Find("entry-1").Failure.ProgressReport.CurrentNodeType).Should(Equal(types.NodeTypeIt))
1078+
1079+
Ω(times.Get("entry-1")).Should(BeNumerically("~", 100*time.Millisecond, 50*time.Millisecond))
1080+
Ω(times.Get("entry-2")).Should(BeNumerically("~", 150*time.Millisecond, 50*time.Millisecond))
1081+
Ω(times.Get("entry-3")).Should(BeNumerically("~", 100*time.Millisecond, 50*time.Millisecond))
1082+
Ω(times.Get("entry-4")).Should(BeNumerically("~", 150*time.Millisecond, 50*time.Millisecond))
10651083
})
1066-
Ω(success).Should(Equal(false))
10671084
})
10681085

1069-
It("should work", func() {
1070-
Ω(rt).Should(HaveTracked("entry-1", "entry-2", "entry-3", "entry-4", "entry-5-entry-5", "entry-6-entry-6"))
1071-
Ω(reporter.Did.Find("entry-1")).Should(HaveTimedOut())
1072-
Ω(reporter.Did.Find("entry-2")).Should(HaveTimedOut())
1073-
Ω(reporter.Did.Find("entry-3")).Should(HaveTimedOut())
1074-
Ω(reporter.Did.Find("entry-4")).Should(HaveTimedOut())
1075-
Ω(reporter.Did.Find("entry-1").Failure.ProgressReport.CurrentNodeType).Should(Equal(types.NodeTypeIt))
1076-
1077-
Ω(times.Get("entry-1")).Should(BeNumerically("~", 100*time.Millisecond, 50*time.Millisecond))
1078-
Ω(times.Get("entry-2")).Should(BeNumerically("~", 150*time.Millisecond, 50*time.Millisecond))
1079-
Ω(times.Get("entry-3")).Should(BeNumerically("~", 100*time.Millisecond, 50*time.Millisecond))
1080-
Ω(times.Get("entry-4")).Should(BeNumerically("~", 150*time.Millisecond, 50*time.Millisecond))
1086+
Describe("the edge case in #1415", func() {
1087+
var four = 4
1088+
var nSix = -6
1089+
DescribeTable("it supports receiving a SpecContext and works with nil parameters", func(ctx context.Context, num *int, s string, cVal string) {
1090+
Ω(ctx).ShouldNot(BeNil())
1091+
if num == nil {
1092+
Ω(s).Should(Equal("nil"))
1093+
} else {
1094+
Ω(s).Should(Equal(strconv.Itoa(*num)))
1095+
}
1096+
if cVal != "" {
1097+
Ω(ctx.Value("key")).Should(Equal(cVal))
1098+
}
1099+
},
1100+
Entry("4", &four, "4", ""),
1101+
Entry("-6", &nSix, "-6", ""),
1102+
Entry("nil", nil, "nil", ""),
1103+
Entry("4 with context value", context.WithValue(context.Background(), "key", "val"), &four, "4", "val"),
1104+
Entry("nil with context value", context.WithValue(context.Background(), "key", "val"), nil, "nil", "val"),
1105+
)
10811106
})
10821107
})
10831108
})

‎internal/internal_integration/table_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ var _ = Describe("Table driven tests", func() {
348348
Ω(b).Should(Equal(c[1]))
349349
Ω(c[2]).Should(BeNil())
350350
}, Entry("variadic arguments", 1, "one", 1, "one", nil))
351+
351352
})
352353

353354
Describe("when table entries are marked pending", func() {

‎table_dsl.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,15 @@ func generateTable(description string, isSubtree bool, args ...interface{}) {
269269
internalNodeArgs = append(internalNodeArgs, entry.decorations...)
270270

271271
hasContext := false
272-
if internalBodyType.NumIn() > 0. {
272+
if internalBodyType.NumIn() > 0 {
273273
if internalBodyType.In(0).Implements(specContextType) {
274274
hasContext = true
275-
} else if internalBodyType.In(0).Implements(contextType) && (len(entry.parameters) == 0 || !reflect.TypeOf(entry.parameters[0]).Implements(contextType)) {
275+
} else if internalBodyType.In(0).Implements(contextType) {
276276
hasContext = true
277+
if len(entry.parameters) > 0 && reflect.TypeOf(entry.parameters[0]) != nil && reflect.TypeOf(entry.parameters[0]).Implements(contextType) {
278+
// we allow you to pass in a non-nil context
279+
hasContext = false
280+
}
277281
}
278282
}
279283

0 commit comments

Comments
 (0)
Please sign in to comment.