aboutsummaryrefslogtreecommitdiffstats
path: root/exercises/concurrent/concurrent1
diff options
context:
space:
mode:
authorMaurĂ­cio Antunes <mauricio.abreua@gmail.com>2024-02-14 13:47:56 -0300
committerMaurĂ­cio Antunes <mauricio.abreua@gmail.com>2024-02-14 13:47:56 -0300
commit3eaddc8b3528c41a7f51dd8f7738344a56ae451e (patch)
tree3e519bc347e5035d2de1ceba535a32064d5db3ec /exercises/concurrent/concurrent1
parent740ad3633665e74ca1df54085b6de940c4b67432 (diff)
feat: New exercises (concurrency)
Diffstat (limited to 'exercises/concurrent/concurrent1')
-rw-r--r--exercises/concurrent/concurrent1/main_test.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/exercises/concurrent/concurrent1/main_test.go b/exercises/concurrent/concurrent1/main_test.go
new file mode 100644
index 0000000..3965113
--- /dev/null
+++ b/exercises/concurrent/concurrent1/main_test.go
@@ -0,0 +1,45 @@
+// concurrent1
+// Make the tests pass!
+
+// I AM NOT DONE
+package main_test
+
+import (
+ "bytes"
+ "fmt"
+ "sync"
+ "testing"
+)
+
+func TestPrinter(t *testing.T) {
+ var buf bytes.Buffer
+ print(&buf)
+
+ out := buf.String()
+
+ for i := 0; i < 3; i++ {
+ want := fmt.Sprintf("Hello from goroutine %d!", i)
+ if !bytes.Contains([]byte(out), []byte(want)) {
+ t.Errorf("Output did not contain expected string. Wanted: %q, Got: %q", want, out)
+ }
+ }
+}
+
+func print(buf *bytes.Buffer) {
+ var wg sync.WaitGroup
+ var mu sync.Mutex
+
+ goroutines := 3
+
+ for i := 0; i < goroutines; i++ {
+ wg.Add(1)
+ go func(i int) {
+ defer wg.Done()
+ mu.Lock()
+ //fmt.Fprintf(buf, "Hello from goroutine %d!\n", i)
+ mu.Unlock()
+ }(i)
+ }
+
+ wg.Wait()
+}