Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): retry event sending when not acked
- Loading branch information
Guillaume Louvigny
committed
Oct 29, 2018
1 parent
5cac515
commit b78c6df
Showing
5 changed files
with
283 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package p2p | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"berty.tech/core/test/mock" | ||
"github.com/jinzhu/gorm" | ||
) | ||
|
||
func setupNonAcknowledgedEventDestinations() (string, *gorm.DB, time.Time, time.Time, time.Time) { | ||
filename, db, _ := mock.GetMockedDb(Event{}) | ||
|
||
now := time.Now() | ||
past := now.Add(-time.Second) | ||
future := now.Add(time.Second) | ||
|
||
db.Save(&Event{ID: "Event1", Direction: Event_Outgoing, ReceiverID: "Receiver1", SentAt: &past}) | ||
db.Save(&Event{ID: "Event2", Direction: Event_Outgoing, ReceiverID: "Receiver1", SentAt: &future}) | ||
db.Save(&Event{ID: "Event3", Direction: Event_Outgoing, ReceiverID: "Receiver2", SentAt: &future}) | ||
|
||
db.Save(&Event{ID: "Event4", Direction: Event_Incoming, ReceiverID: "Receiver1", SentAt: &past}) | ||
db.Save(&Event{ID: "Event5", Direction: Event_Incoming, ReceiverID: "Receiver1", SentAt: &future}) | ||
db.Save(&Event{ID: "Event6", Direction: Event_Incoming, ReceiverID: "Receiver2", SentAt: &future}) | ||
|
||
db.Save(&Event{ID: "Event7", Direction: Event_Outgoing, ConversationID: "Conversation1", SentAt: &past}) | ||
db.Save(&Event{ID: "Event8", Direction: Event_Outgoing, ConversationID: "Conversation1", SentAt: &future}) | ||
db.Save(&Event{ID: "Event9", Direction: Event_Outgoing, ConversationID: "Conversation2", SentAt: &future}) | ||
|
||
db.Save(&Event{ID: "Event10", Direction: Event_Incoming, ConversationID: "Conversation1", SentAt: &past}) | ||
db.Save(&Event{ID: "Event11", Direction: Event_Incoming, ConversationID: "Conversation1", SentAt: &future}) | ||
db.Save(&Event{ID: "Event12", Direction: Event_Incoming, ConversationID: "Conversation2", SentAt: &future}) | ||
|
||
return filename, db, past, now, future | ||
} | ||
|
||
func TestFindNonAcknowledgedEventDestinations(t *testing.T) { | ||
filename, db, _, now, _ := setupNonAcknowledgedEventDestinations() | ||
defer mock.RemoveDb(filename, db) | ||
|
||
expected := map[string]bool{ | ||
"Receiver1:": false, | ||
":Conversation1": false, | ||
} | ||
|
||
destinations, err := FindNonAcknowledgedEventDestinations(db, now) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
for _, destination := range destinations { | ||
identifier := fmt.Sprintf("%s:%s", destination.ReceiverID, destination.ConversationID) | ||
|
||
value, ok := expected[identifier] | ||
if ok == false { | ||
t.Error(fmt.Errorf("%s was not suppoesed to be found", identifier)) | ||
} | ||
|
||
if value == true { | ||
t.Error(fmt.Errorf("%s was found twice", identifier)) | ||
} | ||
|
||
expected[identifier] = true | ||
} | ||
|
||
for identifier, value := range expected { | ||
if value == false { | ||
t.Error(fmt.Errorf("%s was supposed to be found but was not", identifier)) | ||
} | ||
} | ||
} | ||
|
||
func TestFindNonAcknowledgedEventsForDestination(t *testing.T) { | ||
filename, db, _, _, _ := setupNonAcknowledgedEventDestinations() | ||
defer mock.RemoveDb(filename, db) | ||
|
||
expected := map[string]bool{ | ||
"Event1": false, | ||
"Event2": false, | ||
} | ||
|
||
events, err := FindNonAcknowledgedEventsForDestination(db, &Event{ReceiverID: "Receiver1"}) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
for _, event := range events { | ||
value, ok := expected[event.ID] | ||
if ok == false { | ||
t.Error(fmt.Errorf("%s was not suppoesed to be found", event.ID)) | ||
} | ||
|
||
if value == true { | ||
t.Error(fmt.Errorf("%s was found twice", event.ID)) | ||
} | ||
|
||
expected[event.ID] = true | ||
} | ||
|
||
for identifier, value := range expected { | ||
if value == false { | ||
t.Error(fmt.Errorf("%s was supposed to be found but was not", identifier)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"berty.tech/core/entity" | ||
|
||
networkmock "berty.tech/core/network/mock" | ||
"github.com/pkg/errors" | ||
|
||
"berty.tech/core/api/p2p" | ||
) | ||
|
||
func setupNonAcknowledgedEventDestinations() (*AppMock, time.Time, time.Time, time.Time) { | ||
n, err := NewAppMock(&entity.Device{Name: "Alice's iPhone"}, networkmock.NewEnqueuer(), WithUnencryptedDb()) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
now := time.Now() | ||
past := now.Add(-time.Second) | ||
future := now.Add(time.Second) | ||
|
||
n.db.Save(&p2p.Event{ID: "Event1", Direction: p2p.Event_Outgoing, ReceiverID: "Receiver1", SentAt: &past}) | ||
n.db.Save(&p2p.Event{ID: "Event2", Direction: p2p.Event_Outgoing, ReceiverID: "Receiver1", SentAt: &future}) | ||
n.db.Save(&p2p.Event{ID: "Event3", Direction: p2p.Event_Outgoing, ReceiverID: "Receiver2", SentAt: &future}) | ||
|
||
n.db.Save(&p2p.Event{ID: "Event4", Direction: p2p.Event_Incoming, ReceiverID: "Receiver1", SentAt: &past}) | ||
n.db.Save(&p2p.Event{ID: "Event5", Direction: p2p.Event_Incoming, ReceiverID: "Receiver1", SentAt: &future}) | ||
n.db.Save(&p2p.Event{ID: "Event6", Direction: p2p.Event_Incoming, ReceiverID: "Receiver2", SentAt: &future}) | ||
|
||
n.db.Save(&p2p.Event{ID: "Event7", Direction: p2p.Event_Outgoing, ConversationID: "Conversation1", SentAt: &past}) | ||
n.db.Save(&p2p.Event{ID: "Event8", Direction: p2p.Event_Outgoing, ConversationID: "Conversation1", SentAt: &future}) | ||
n.db.Save(&p2p.Event{ID: "Event9", Direction: p2p.Event_Outgoing, ConversationID: "Conversation2", SentAt: &future}) | ||
|
||
n.db.Save(&p2p.Event{ID: "Event10", Direction: p2p.Event_Incoming, ConversationID: "Conversation1", SentAt: &past}) | ||
n.db.Save(&p2p.Event{ID: "Event11", Direction: p2p.Event_Incoming, ConversationID: "Conversation1", SentAt: &future}) | ||
n.db.Save(&p2p.Event{ID: "Event12", Direction: p2p.Event_Incoming, ConversationID: "Conversation2", SentAt: &future}) | ||
|
||
return n, past, now, future | ||
} | ||
|
||
func TestEventRetry(t *testing.T) { | ||
appMock, _, now, _ := setupNonAcknowledgedEventDestinations() | ||
defer appMock.Close() | ||
|
||
expected := map[string]bool{ | ||
"Event1": false, | ||
"Event2": false, | ||
"Event7": false, | ||
"Event8": false, | ||
} | ||
|
||
events, err := appMock.node.EventRetry(now) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
for _, event := range events { | ||
value, ok := expected[event.ID] | ||
if ok == false { | ||
t.Error(errors.New(fmt.Sprintf("%s was not suppoesed to be found", event.ID))) | ||
} | ||
|
||
if value == true { | ||
t.Error(errors.New(fmt.Sprintf("%s was found twice", event.ID))) | ||
} | ||
|
||
expected[event.ID] = true | ||
} | ||
|
||
for identifier, value := range expected { | ||
if value == false { | ||
t.Error(errors.New(fmt.Sprintf("%s was supposed to be found but was not", identifier))) | ||
} | ||
} | ||
|
||
} |