-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
146 lines (120 loc) · 2.86 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"database/sql"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"time"
_ "github.com/boz/ephemerald/builtin/postgres"
_ "github.com/boz/ephemerald/builtin/redis"
_ "github.com/lib/pq"
"github.com/Sirupsen/logrus"
"github.com/boz/ephemerald/net"
"github.com/garyburd/redigo/redis"
)
func main() {
log := logrus.New()
builder := net.NewClientBuilder()
client, err := builder.Create()
if err != nil {
log.WithError(err).Fatal("can't create client")
}
var wg sync.WaitGroup
passed := uint32(0)
failed := uint32(0)
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
secs := time.Duration(rand.Intn(5)) * time.Second
time.Sleep(secs)
var redisUrl string
var pgUrl string
if i%2 == 0 {
// multi checkout
items, err := client.CheckoutBatch()
if err != nil {
log.WithError(err).Error("checkout multi")
return
}
defer func() {
if err := client.ReturnBatch(items); err != nil {
log.WithError(err).Error("return multi")
}
}()
pgUrl = items["postgres"].Url
redisUrl = items["redis"].Url
} else {
// single checkout
rparam, err := client.Checkout("redis")
if err != nil {
log.WithError(err).Error("checkout redis")
return
}
defer func() {
if err := client.Return("redis", rparam); err != nil {
log.WithError(err).Error("return redis")
}
}()
redisUrl = rparam.Url
pgparam, err := client.Checkout("postgres")
if err != nil {
log.WithError(err).Error("checkout postgres")
return
}
defer func() {
if err := client.Return("postgres", rparam); err != nil {
log.WithError(err).Error("return postgres")
}
}()
pgUrl = pgparam.Url
}
// connect to redis instance
rconn, err := redis.DialURL(redisUrl)
if err != nil {
log.WithError(err).Error("dialing redis")
return
}
defer rconn.Close()
// connect to pg
pconn, err := sql.Open("postgres", pgUrl)
if err != nil {
log.WithError(err).Error("dialing postgres")
return
}
defer pconn.Close()
// run tests
err = runTests(rconn, pconn)
if err != nil {
atomic.AddUint32(&failed, uint32(1))
log.WithError(err).Error("test failed")
} else {
atomic.AddUint32(&passed, uint32(1))
log.Info("test passed")
}
}()
}
wg.Wait()
total := passed + failed
log.WithField("total", total).
WithField("passed", passed).
WithField("failed", failed).
Infof("Tests complete")
}
func runTests(rconn redis.Conn, pconn *sql.DB) error {
res, err := pconn.Exec("INSERT INTO users (name) VALUES($1);", "foo")
if err != nil {
return err
}
count, err := res.RowsAffected()
if err != nil {
return err
}
if count != 1 {
return fmt.Errorf("invalid rows affected: %v != %v", 1, count)
}
secs := time.Duration(rand.Intn(5)*500) * time.Millisecond
time.Sleep(secs)
return nil
}