-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmass_test.go
138 lines (116 loc) · 2.44 KB
/
mass_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
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
// Tideland Go Actor - Unit Tests
//
// Copyright (C) 2019-2023 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package actor_test
//--------------------
// IMPORTS
//--------------------
import (
"math/rand"
"testing"
"time"
"tideland.dev/go/audit/asserts"
"tideland.dev/go/actor"
)
//--------------------
// TESTS
//--------------------
// TestMass verifies the starting and stopping an Actor.
func TestMass(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
pps := make([]*PingPong, 1000)
for i := 0; i < len(pps); i++ {
pps[i] = NewPingPong(pps)
}
// Let's start the ping pong party.
for i := 0; i < 5; i++ {
n := rand.Intn(len(pps))
pps[n].Ping()
n = rand.Intn(len(pps))
pps[n].Pong()
}
// Let's wait one seconds before stopping.
time.Sleep(1 * time.Second)
// Let's check some random ping pong pairs.
for _, pp := range pps {
pings, pongs := pp.PingPongs()
assert.True(pings > 0)
assert.True(pongs > 0)
pp.Stop()
}
}
// TestPerformance verifies the starting and stopping an Actor.
func TestPerformance(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
finalized := make(chan struct{})
act, err := actor.Go(actor.WithFinalizer(func(err error) error {
defer close(finalized)
return err
}))
assert.OK(err)
assert.NotNil(act)
now := time.Now()
for i := 0; i < 10000; i++ {
act.DoAsync(func() {})
}
duration := time.Since(now)
assert.True(duration < 100*time.Millisecond)
act.Stop()
<-finalized
assert.NoError(act.Err())
}
//--------------------
// TEST ACTOR
//--------------------
type PingPong struct {
pps []*PingPong
pings int
pongs int
act *actor.Actor
}
func NewPingPong(pps []*PingPong) *PingPong {
pp := &PingPong{
pps: pps,
pings: 0,
pongs: 0,
}
act, err := actor.Go(actor.WithQueueCap(256))
if err != nil {
panic(err)
}
pp.act = act
return pp
}
func (pp *PingPong) Ping() {
pp.act.DoAsync(func() {
pp.pings++
n := rand.Intn(len(pp.pps))
pp.pps[n].Pong()
})
}
func (pp *PingPong) Pong() {
pp.act.DoAsync(func() {
pp.pongs++
n := rand.Intn(len(pp.pps))
pp.pps[n].Ping()
})
}
func (pp *PingPong) PingPongs() (int, int) {
var pings int
var pongs int
pp.act.DoSync(func() {
pings = pp.pings
pongs = pp.pongs
})
return pings, pongs
}
func (pp *PingPong) Err() error {
return pp.act.Err()
}
func (pp *PingPong) Stop() {
pp.act.Stop()
}
// EOF