Skip to content

Commit 91b8deb

Browse files
committed
Refactor test context creation to be less error-prone
Change-Id: I176faf21ff3f2cac5e9d8af0e54219761693d361
1 parent 24692d0 commit 91b8deb

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

test/e2e/tests/setup_e2e_test.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ var (
6565
// Some architectures don't have local ssd. Give way to opt out of tests like datacache.
6666
skipLocalSsdTests = flag.Bool("skip-local-ssd-tests", false, "Skip local ssd tests like datacache")
6767

68-
testContexts = []*remote.TestContext{}
69-
hyperdiskTestContexts = []*remote.TestContext{}
68+
testContexts []*remote.TestContext
69+
hyperdiskTestContexts []*remote.TestContext
7070
computeService *compute.Service
7171
computeAlphaService *computealpha.Service
7272
computeBetaService *computebeta.Service
@@ -87,10 +87,6 @@ var _ = BeforeSuite(func() {
8787
var err error
8888
numberOfInstancesPerZone := 2
8989
zones := strings.Split(*zones, ",")
90-
tcc := make(chan *remote.TestContext, len(zones)*numberOfInstancesPerZone)
91-
hdtcc := make(chan *remote.TestContext, len(zones))
92-
defer close(tcc)
93-
defer close(hdtcc)
9490

9591
rand.Seed(time.Now().UnixNano())
9692

@@ -116,42 +112,43 @@ var _ = BeforeSuite(func() {
116112

117113
klog.Infof("Running in project %v with service account %v", *project, *serviceAccount)
118114

119-
setupContext := func(zone string) {
115+
testContexts = make([]*remote.TestContext, numberOfInstancesPerZone*len(zones))
116+
if *hdMinCpuPlatform != noMachineType {
117+
hyperdiskTestContexts = make([]*remote.TestContext, len(zones))
118+
}
119+
var zoneGroup sync.WaitGroup
120+
setupContext := func(idx int, zone string) {
121+
defer zoneGroup.Done()
120122
var wg sync.WaitGroup
121123
// Create 2 instances for each zone as we need 2 instances each zone for certain test cases
122124
for j := 0; j < numberOfInstancesPerZone; j++ {
123125
wg.Add(1)
124126
go func(curZone string, randInt int) {
125127
defer GinkgoRecover()
126128
defer wg.Done()
127-
tcc <- NewDefaultTestContext(curZone, strconv.Itoa(randInt))
129+
tc := NewDefaultTestContext(curZone, strconv.Itoa(randInt))
130+
testContexts[j+idx*numberOfInstancesPerZone] = tc
131+
klog.Infof("Added TestContext for node %s", tc.Instance.GetName())
128132
}(zone, j)
129133
}
130134
if *hdMachineType != noMachineType {
131135
wg.Add(1)
132136
go func(curZone string) {
133137
defer GinkgoRecover()
134138
defer wg.Done()
135-
hdtcc <- NewTestContext(curZone, *hdMinCpuPlatform, *hdMachineType, "0")
139+
tc := NewTestContext(curZone, *hdMinCpuPlatform, *hdMachineType, "0")
140+
hyperdiskTestContexts[idx] = tc
141+
klog.Infof("Added hyperdisk TestContext for node %s", tc.Instance.GetName())
136142
}(zone)
137143
}
138144
wg.Wait()
139145
}
140146

141-
for _, zone := range zones {
142-
setupContext(zone)
143-
}
144-
145-
for i := 0; i < len(zones)*numberOfInstancesPerZone; i++ {
146-
tc := <-tcc
147-
testContexts = append(testContexts, tc)
148-
klog.Infof("Added TestContext for node %s", tc.Instance.GetName())
149-
}
150-
for i := 0; i < len(zones); i++ {
151-
tc := <-hdtcc
152-
hyperdiskTestContexts = append(hyperdiskTestContexts, tc)
153-
klog.Infof("Added TestContext for node %s", tc.Instance.GetName())
147+
for i, zone := range zones {
148+
zoneGroup.Add(1)
149+
go setupContext(i, zone)
154150
}
151+
zoneGroup.Wait()
155152
})
156153

157154
var _ = AfterSuite(func() {

0 commit comments

Comments
 (0)