-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathpublication_test.go
174 lines (139 loc) · 5.39 KB
/
publication_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Copyright 2016 Stanislav Liberman
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package aeron
import (
"github.com/lirm/aeron-go/aeron/atomic"
"github.com/lirm/aeron-go/aeron/counters"
"github.com/lirm/aeron-go/aeron/driver"
"github.com/lirm/aeron-go/aeron/logbuffer"
"github.com/lirm/aeron-go/aeron/ringbuffer"
"github.com/lirm/aeron-go/aeron/util"
"github.com/lirm/aeron-go/aeron/util/memmap"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
"time"
)
func prepareCnc(t *testing.T) (string, *counters.MetaDataFlyweight) {
counterFileName := "cnc.dat"
mmap, err := memmap.NewFile(counterFileName, 0, 256*1024)
require.NoError(t, err)
cncBuffer := atomic.MakeBuffer(mmap.GetMemoryPtr(), mmap.GetMemorySize())
meta := counters.InitAndWrapMetaData(cncBuffer, 0, 1024, 1024, 0, 0, 0)
t.Logf("meta data: %v", meta)
return counterFileName, meta
}
func TestNumberOfZeros(t *testing.T) {
assert.EqualValues(t, util.NumberOfTrailingZeroes(65536), 16)
assert.EqualValues(t, util.NumberOfTrailingZeroes(131072), 17)
assert.EqualValues(t, util.NumberOfTrailingZeroes(4194304), 22)
}
func TestNewPublication(t *testing.T) {
cncName, meta := prepareCnc(t)
defer require.NoError(t, os.Remove(cncName))
var proxy driver.Proxy
var rb rb.ManyToOne
buf := meta.ToDriverBuf.Get()
require.NotNil(t, buf)
t.Logf("RingBuffer backing atomic.Buffer: %v", buf)
rb.Init(buf)
proxy.Init(&rb)
var cc ClientConductor
cc.Init(&proxy, nil, time.Millisecond*100, time.Millisecond*100, time.Millisecond*100, time.Millisecond*100, meta)
defer require.NoError(t, cc.Close())
lb, err := logbuffer.NewTestingLogbuffer()
defer require.NoError(t, logbuffer.RemoveTestingLogbufferFile())
require.NoError(t, err)
lb.Meta().MTULen.Set(8192)
pub := NewPublication(lb)
pub.conductor = &cc
pub.channel = "aeron:ipc"
pub.regID = 1
pub.streamID = 10
pub.sessionID = 100
metaBuffer := lb.Buffer(logbuffer.PartitionCount)
if nil == metaBuffer {
t.Logf("meta: %v", metaBuffer)
}
counter := atomic.MakeBuffer(make([]byte, 256))
pub.pubLimit = NewPosition(counter, 0)
t.Logf("pub: %v", pub.pubLimit)
require.EqualValues(t, pub.pubLimit.get(), 0)
srcBuffer := atomic.MakeBuffer(make([]byte, 256))
milliEpoch := (time.Nanosecond * time.Duration(time.Now().UnixNano())) / time.Millisecond
pos := pub.Offer(srcBuffer, 0, srcBuffer.Capacity(), nil)
t.Logf("new pos: %d", pos)
assert.Equalf(t, pos, NotConnected, "Expected publication to not be connected at %d: %d", milliEpoch, pos)
//pub.metaData.TimeOfLastStatusMsg.Set(milliEpoch.Nanoseconds())
//pos = pub.Offer(srcBuffer, 0, srcBuffer.Capacity(), nil)
//t.Logf("new pos: %d", pos)
//if pos != BackPressured {
// t.Errorf("Expected publication to be back pressured: %d", pos)
//}
//
pub.pubLimit.set(1024)
pos = pub.Offer(srcBuffer, 0, srcBuffer.Capacity(), nil)
t.Logf("new pos: %d", pos)
assert.EqualValuesf(t, pos, srcBuffer.Capacity()+logbuffer.DataFrameHeader.Length,
"Expected publication to advance to position %d", srcBuffer.Capacity()+logbuffer.DataFrameHeader.Length)
}
func TestPublication_Offer(t *testing.T) {
cncName, meta := prepareCnc(t)
defer require.NoError(t, os.Remove(cncName))
var proxy driver.Proxy
var rb rb.ManyToOne
buf := meta.ToDriverBuf.Get()
require.NotNil(t, buf)
t.Logf("RingBuffer backing atomic.Buffer: %v", buf)
rb.Init(buf)
proxy.Init(&rb)
var cc ClientConductor
cc.Init(&proxy, nil, time.Millisecond*100, time.Millisecond*100, time.Millisecond*100, time.Millisecond*100, meta)
defer require.NoError(t, cc.Close())
lb, err := logbuffer.NewTestingLogbuffer()
defer require.NoError(t, logbuffer.RemoveTestingLogbufferFile())
require.NoError(t, err)
lb.Meta().MTULen.Set(8192)
pub := NewPublication(lb)
pub.conductor = &cc
pub.channel = "aeron:ipc"
pub.regID = 1
pub.streamID = 10
pub.sessionID = 100
metaBuffer := lb.Buffer(logbuffer.PartitionCount)
if nil == metaBuffer {
t.Logf("meta: %v", metaBuffer)
}
counter := atomic.MakeBuffer(make([]byte, 256))
pub.pubLimit = NewPosition(counter, 0)
t.Logf("pub: %v", pub.pubLimit)
require.EqualValues(t, pub.pubLimit.get(), 0)
srcBuffer := atomic.MakeBuffer(make([]byte, 256))
//milliEpoch := (time.Nanosecond * time.Duration(time.Now().UnixNano())) / time.Millisecond
//pub.metaData.TimeOfLastStatusMsg.Set(milliEpoch.Nanoseconds())
termBufLen := lb.Buffer(0).Capacity()
t.Logf("Term buffer length: %d", termBufLen)
frameLen := int64(srcBuffer.Capacity() + logbuffer.DataFrameHeader.Length)
pub.pubLimit.set(int64(termBufLen))
for i := int64(1); i <= int64(termBufLen)/frameLen; i++ {
pos := pub.Offer(srcBuffer, 0, srcBuffer.Capacity(), nil)
//t.Logf("new pos: %d", pos)
assert.Equalf(t, pos, frameLen*i,
"Expected publication to advance to position %d (have %d)", frameLen*i, pos)
}
pos := pub.Offer(srcBuffer, 0, srcBuffer.Capacity(), nil)
t.Logf("new pos: %d", pos)
assert.Equalf(t, pos, AdminAction,
"Expected publication to trigger AdminAction (%d)", pos)
}