@@ -2,6 +2,7 @@ package centrifuge
22
33import (
44 "context"
5+ "github.com/google/uuid"
56 "os"
67 "strconv"
78 "testing"
@@ -147,7 +148,7 @@ func TestMemoryBrokerPublishIdempotent(t *testing.T) {
147148 },
148149 }
149150
150- // Test publish with history and with idempotency key.
151+ // Test publish with idempotency key.
151152 _ , _ , err := e .Publish ("channel" , testPublicationData (), PublishOptions {
152153 IdempotencyKey : "test" ,
153154 })
@@ -212,6 +213,67 @@ func TestMemoryBrokerPublishIdempotentWithHistory(t *testing.T) {
212213 require .Equal (t , 1 , numPubs )
213214}
214215
216+ func TestMemoryBrokerPublishSkipOldVersion (t * testing.T ) {
217+ e := testMemoryBroker ()
218+ defer func () { _ = e .node .Shutdown (context .Background ()) }()
219+
220+ numPubs := 0
221+
222+ e .eventHandler = & testBrokerEventHandler {
223+ HandlePublicationFunc : func (ch string , pub * Publication , sp StreamPosition , delta bool , prevPub * Publication ) error {
224+ numPubs ++
225+ return nil
226+ },
227+ }
228+
229+ channel1 := uuid .NewString ()
230+
231+ // Test publish with history and with version.
232+ _ , _ , err := e .Publish (channel1 , testPublicationData (), PublishOptions {
233+ HistorySize : 1 ,
234+ HistoryTTL : time .Second ,
235+ Version : 1 ,
236+ })
237+ require .NoError (t , err )
238+ // Publish with same version.
239+ _ , _ , err = e .Publish (channel1 , testPublicationData (), PublishOptions {
240+ HistorySize : 1 ,
241+ HistoryTTL : time .Second ,
242+ Version : 1 ,
243+ })
244+ require .NoError (t , err )
245+ require .Equal (t , 1 , numPubs )
246+
247+ numPubs = 0
248+ channel2 := uuid .NewString ()
249+ // Test publish with history and with version and version epoch.
250+ _ , _ , err = e .Publish (channel2 , testPublicationData (), PublishOptions {
251+ HistorySize : 1 ,
252+ HistoryTTL : time .Second ,
253+ Version : 1 ,
254+ VersionEpoch : "xyz" ,
255+ })
256+ require .NoError (t , err )
257+ // Publish with same version and epoch.
258+ _ , _ , err = e .Publish (channel2 , testPublicationData (), PublishOptions {
259+ HistorySize : 1 ,
260+ HistoryTTL : time .Second ,
261+ Version : 1 ,
262+ VersionEpoch : "xyz" ,
263+ })
264+ require .NoError (t , err )
265+ require .Equal (t , 1 , numPubs )
266+ // Publish with same version and different epoch.
267+ _ , _ , err = e .Publish (channel2 , testPublicationData (), PublishOptions {
268+ HistorySize : 1 ,
269+ HistoryTTL : time .Second ,
270+ Version : 1 ,
271+ VersionEpoch : "aaa" ,
272+ })
273+ require .NoError (t , err )
274+ require .Equal (t , 2 , numPubs )
275+ }
276+
215277func TestMemoryEngineSubscribeUnsubscribe (t * testing.T ) {
216278 e := testMemoryBroker ()
217279 defer func () { _ = e .node .Shutdown (context .Background ()) }()
@@ -229,10 +291,10 @@ func TestMemoryHistoryHub(t *testing.T) {
229291 ch1 := "channel1"
230292 ch2 := "channel2"
231293 pub := newTestPublication ()
232- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
233- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
234- _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second })
235- _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second })
294+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
295+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
296+ _ , _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second })
297+ _ , _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second })
236298
237299 hist , _ , err := h .get (ch1 , HistoryOptions {
238300 Filter : HistoryFilter {
@@ -271,10 +333,10 @@ func TestMemoryHistoryHub(t *testing.T) {
271333 require .Equal (t , 0 , len (hist ))
272334
273335 // test history messages limit
274- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 10 , HistoryTTL : time .Second })
275- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 10 , HistoryTTL : time .Second })
276- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 10 , HistoryTTL : time .Second })
277- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 10 , HistoryTTL : time .Second })
336+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 10 , HistoryTTL : time .Second })
337+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 10 , HistoryTTL : time .Second })
338+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 10 , HistoryTTL : time .Second })
339+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 10 , HistoryTTL : time .Second })
278340 hist , _ , err = h .get (ch1 , HistoryOptions {
279341 Filter : HistoryFilter {
280342 Limit : - 1 ,
@@ -291,8 +353,8 @@ func TestMemoryHistoryHub(t *testing.T) {
291353 require .Equal (t , 1 , len (hist ))
292354
293355 // test history limit greater than history size
294- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
295- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
356+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
357+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
296358 hist , _ , err = h .get (ch1 , HistoryOptions {
297359 Filter : HistoryFilter {
298360 Limit : 2 ,
@@ -312,10 +374,10 @@ func TestMemoryHistoryHubMetaTTL(t *testing.T) {
312374 h .RLock ()
313375 require .Equal (t , int64 (0 ), h .nextRemoveCheck )
314376 h .RUnlock ()
315- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
316- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
317- _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second })
318- _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second })
377+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
378+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second })
379+ _ , _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second })
380+ _ , _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second })
319381 h .RLock ()
320382 require .True (t , h .nextRemoveCheck > 0 )
321383 require .Equal (t , 2 , len (h .streams ))
@@ -350,10 +412,10 @@ func TestMemoryHistoryHubMetaTTLPerChannel(t *testing.T) {
350412 h .RLock ()
351413 require .Equal (t , int64 (0 ), h .nextRemoveCheck )
352414 h .RUnlock ()
353- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second , HistoryMetaTTL : time .Second })
354- _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second , HistoryMetaTTL : time .Second })
355- _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second , HistoryMetaTTL : time .Second })
356- _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second , HistoryMetaTTL : time .Second })
415+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second , HistoryMetaTTL : time .Second })
416+ _ , _ , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second , HistoryMetaTTL : time .Second })
417+ _ , _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second , HistoryMetaTTL : time .Second })
418+ _ , _ , _ , _ = h .add (ch2 , pub , PublishOptions {HistorySize : 2 , HistoryTTL : time .Second , HistoryMetaTTL : time .Second })
357419 h .RLock ()
358420 require .True (t , h .nextRemoveCheck > 0 )
359421 require .Equal (t , 2 , len (h .streams ))
@@ -784,8 +846,8 @@ func TestMemoryHistoryHubPrevPub(t *testing.T) {
784846 defer h .close ()
785847 ch1 := "channel1"
786848 pub := newTestPublication ()
787- _ , prevPub , _ := h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second , UseDelta : true })
849+ _ , prevPub , _ , _ := h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second , UseDelta : true })
788850 require .Nil (t , prevPub )
789- _ , prevPub , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second , UseDelta : true })
851+ _ , prevPub , _ , _ = h .add (ch1 , pub , PublishOptions {HistorySize : 1 , HistoryTTL : time .Second , UseDelta : true })
790852 require .NotNil (t , prevPub )
791853}
0 commit comments