Logging needs to be initialized in order for the state unit tests
to function without issue.
See previous commit ("Fix config unit tests") for more information.
Signed-off-by: Ava Affine <ava@sunnypup.io>
222 lines
4.5 KiB
Go
222 lines
4.5 KiB
Go
package state
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitlab.com/whom/bingobot/internal/logging"
|
|
)
|
|
|
|
/* WARNING:
|
|
* Cannot run these tests in parallel!
|
|
* limitation of SetupTest and CleanupTest
|
|
*/
|
|
|
|
const TestTok = "TEST_NAME"
|
|
var loggingInitialized = false
|
|
|
|
func SetupTest(t *testing.T) {
|
|
// have to set up logger
|
|
if !loggingInitialized {
|
|
logging.Init()
|
|
loggingInitialized = true
|
|
}
|
|
|
|
old, _ := time.Parse(
|
|
time.RFC3339,
|
|
VeryOldVote,
|
|
)
|
|
|
|
for i := range 270 {
|
|
if err := PublishEvent(UserActiveEvent{UserEvent{
|
|
uid: fmt.Sprintf("%d", i),
|
|
created: old,
|
|
}}); err != nil {
|
|
t.Errorf("Failed to add event: %e", err)
|
|
}
|
|
}
|
|
|
|
PublishEvent(UserActiveEvent{UserEvent{
|
|
uid: fmt.Sprintf(TestTok),
|
|
created: time.Now(),
|
|
}})
|
|
|
|
PublishEvent(ChallengeEvent{UserEvent{
|
|
uid: fmt.Sprintf(TestTok),
|
|
created: time.Now(),
|
|
}})
|
|
|
|
if len(eventCache) != 272 {
|
|
t.Errorf("Unexpected number of events in cache: %d",
|
|
len(eventCache))
|
|
}
|
|
}
|
|
|
|
func CleanupTest() {
|
|
eventSubscriptionCache = [NumEventTypes][]chan Event{}
|
|
eventCache = []Event{}
|
|
}
|
|
|
|
func TestPubSub(t *testing.T) {
|
|
SetupTest(t)
|
|
|
|
c, e := UserActive.SubscribeWithHistory()
|
|
if e != nil {
|
|
t.Errorf("Error subscribing to UserActive events: %e", e)
|
|
}
|
|
|
|
Loop:
|
|
for i := 0; true; i++ {
|
|
select {
|
|
case e, ok := <-c:
|
|
if !ok {
|
|
t.Errorf("Subscription Channel Closed")
|
|
}
|
|
if e.Type() != UserActive {
|
|
t.Errorf("Non UserActive Event in UserActive subscription: %v", e.Type())
|
|
}
|
|
default:
|
|
if i == eventChannelBufferSize {
|
|
break Loop
|
|
} else {
|
|
t.Errorf("Unexpected number of events in channel: %d", i)
|
|
}
|
|
}
|
|
}
|
|
|
|
PublishEvent(UserActiveEvent{UserEvent{
|
|
uid: "uniqueToken",
|
|
created: time.Now(),
|
|
}})
|
|
|
|
select {
|
|
case e, ok := <-c:
|
|
if !ok || e.Data()[UserEventUserKey] != "uniqueToken" {
|
|
t.Errorf("didnt read correct event from channel: %v", e)
|
|
}
|
|
default:
|
|
t.Errorf("New event not published to subscription!")
|
|
}
|
|
|
|
CleanupTest()
|
|
}
|
|
|
|
func TestFilterCache(t *testing.T) {
|
|
SetupTest(t)
|
|
|
|
events, err := GetMatchingEvents(
|
|
UserActive,
|
|
map[string]string{
|
|
UserEventUserKey: TestTok,
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
t.Errorf("Error filtering events: %e", err)
|
|
}
|
|
|
|
if len(events) != 1 {
|
|
t.Errorf("Got too many events from filter: %d", len(events))
|
|
}
|
|
|
|
if events[0].Type() != UserActive {
|
|
t.Errorf("Got wrong event!: %+v", events[0])
|
|
}
|
|
|
|
CleanupTest()
|
|
}
|
|
|
|
func TestPruneCache(t *testing.T) {
|
|
SetupTest(t)
|
|
pruneEventCache()
|
|
|
|
if len(eventCache) != 2 {
|
|
t.Errorf("Incorrect number of remaining events: %d", len(eventCache))
|
|
}
|
|
|
|
CleanupTest()
|
|
}
|
|
|
|
func TestVoteEventValidations(t *testing.T) {
|
|
var err error
|
|
|
|
if err = VoteEvent(
|
|
map[string]string{
|
|
VoteRequesterKey: "r",
|
|
VoteCreatedKey: "c",
|
|
VoteStatusKey: VoteStatusInProgress,
|
|
},
|
|
).Validate(); err.Error() != VoteMissingKeyError+VoteActionKey {
|
|
t.Errorf("Unexpected error from validation: %e", err)
|
|
}
|
|
|
|
if err = VoteEvent(
|
|
map[string]string{
|
|
VoteActionKey: "a",
|
|
VoteRequesterKey: "r",
|
|
VoteCreatedKey: "c",
|
|
VoteStatusKey: VoteStatusInProgress,
|
|
},
|
|
).Validate(); err != nil {
|
|
t.Errorf("Unexpected error: %e", err)
|
|
}
|
|
|
|
if err = VoteEvent(
|
|
map[string]string{
|
|
VoteActionKey: "a",
|
|
VoteRequesterKey: "r",
|
|
VoteCreatedKey: "c",
|
|
VoteStatusKey: "s",
|
|
},
|
|
).Validate(); err.Error() != VoteBadStatusError+"s" {
|
|
t.Errorf("Unexpected or no error: %e", err)
|
|
}
|
|
|
|
if err = VoteEvent(
|
|
map[string]string{
|
|
VoteActionKey: "a",
|
|
VoteRequesterKey: "r",
|
|
VoteCreatedKey: "c",
|
|
VoteStatusKey: VoteStatusInProgress,
|
|
VoteResultKey: VoteResultFail,
|
|
},
|
|
).Validate(); err.Error() != VoteNotFinishedError {
|
|
t.Errorf("Unexpected or no error: %e", err)
|
|
}
|
|
|
|
if err = VoteEvent(
|
|
map[string]string{
|
|
VoteActionKey: "a",
|
|
VoteRequesterKey: "r",
|
|
VoteCreatedKey: "c",
|
|
VoteStatusKey: VoteStatusFinalized,
|
|
},
|
|
).Validate(); err.Error() != VoteMissingResultError {
|
|
t.Errorf("Unexpected or no error: %e", err)
|
|
}
|
|
|
|
if err = VoteEvent(
|
|
map[string]string{
|
|
VoteActionKey: "a",
|
|
VoteRequesterKey: "r",
|
|
VoteCreatedKey: "c",
|
|
VoteStatusKey: VoteStatusFinalized,
|
|
VoteResultKey: "r",
|
|
},
|
|
).Validate(); err.Error() != VoteBadResultError+"r" {
|
|
t.Errorf("Unexpected or no error: %e", err)
|
|
}
|
|
|
|
if err = VoteEvent(
|
|
map[string]string{
|
|
VoteActionKey: "a",
|
|
VoteRequesterKey: "r",
|
|
VoteCreatedKey: "c",
|
|
VoteStatusKey: VoteStatusFinalized,
|
|
VoteResultKey: VoteResultFail,
|
|
},
|
|
).Validate(); err != nil {
|
|
t.Errorf("Unexpected or no error: %e", err)
|
|
}
|
|
}
|