-
Notifications
You must be signed in to change notification settings - Fork 1
/
hartman_test.go
62 lines (54 loc) · 1.12 KB
/
hartman_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package hartman
import (
"context"
"fmt"
"log"
"math/rand"
"testing"
"time"
)
/**
Will try make sandwich
need tomoto shopper
need cheese shopper
need waiter
**/
func TShopper(ctx context.Context) error {
select {
case <-time.After(5 * time.Second):
log.Println("shopped tomatoes")
return nil
case <-ctx.Done():
return fmt.Errorf("could not shop tomatoes")
}
}
func CShopper(ctx context.Context) error {
select {
case <-time.After(4 * time.Second):
r := rand.New(rand.NewSource(time.Now().UnixNano()))
if r.Intn(100)%2 == 0 {
return fmt.Errorf("cheese is not available. sorry, no sandwich")
}
log.Println("shopped cheese")
return nil
case <-ctx.Done():
return fmt.Errorf("could not shop cheese")
}
}
func SWaiter(ctx context.Context) error {
select {
case <-time.After(10 * time.Second):
log.Printf("sandwich is ready")
return nil
case <-ctx.Done():
return fmt.Errorf("order cancelled")
}
}
func TestRunC(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
err := RunC(ctx, TShopper, CShopper, SWaiter)
if err != nil {
t.Error(err)
}
}