Skip to content

Commit

Permalink
Add tests for WaitGroup
Browse files Browse the repository at this point in the history
Test 1 shows that without WaitGroup, no waiting for goroutine. Test 2 shows that WaitGroup successfully forces a join.
  • Loading branch information
JothamWong committed Apr 12, 2024
1 parent e0a1926 commit a6047b3
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,57 @@ v.X // 3
defaultNumWords
);

// Test WaitGroups

testProgram(`
type Vertex struct {
X int
}
func (v *Vertex) Add(w Vertex) {
v.X = v.X + w.X;
}
v := Vertex{1};
w := Vertex{2};
go func() {
for i := 0; i < 1000; i++ { // iterate for 1000 times to prove that waitgroup works as intended
}
v.Add(w);
}()
v.X; //1
`, 1, '', defaultNumWords);

testProgram(`
type Vertex struct {
X int
}
func (v *Vertex) Add(w Vertex) {
v.X = v.X + w.X;
}
v := Vertex{1};
w := Vertex{2};
wg := WaitGroup{1};
go func() {
for i := 0; i < 1000; i++ { // iterate for 1000 times to prove that waitgroup works as intended
}
v.Add(w);
wg.Done();
}()
wg.Wait();
v.X; //3
`, 3, '', defaultNumWords);

// Testing struct methods with goroutines (goroutines cannot return anything)
testProgram(
`
Expand Down

0 comments on commit a6047b3

Please sign in to comment.