Skip to content

Commit

Permalink
Adding new RandomInt utility + updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lleadbet committed Mar 19, 2021
1 parent 9dcc2c8 commit 4dc7c54
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
17 changes: 9 additions & 8 deletions internal/events/types/hype_train/hype_train_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
var event []byte
var err error
lastUser := util.RandomUserID()
lastTotal := util.RandomViewerCount()
lastTotal := util.RandomInt(10 * 100)
lastType := util.RandomType()

//Local variables which will be used for the trigger params below
localTotal := util.RandomViewerCount()
localGoal := util.RandomViewerCount()
localTotal := util.RandomInt(10 * 100)
localGoal := util.RandomInt(10*100*100) + localTotal
localProgress := (localTotal / localGoal)

switch params.Transport {
Expand All @@ -63,12 +64,12 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
Total: util.RandomViewerCount(),
Total: localTotal,
Progress: localProgress,
Goal: localGoal,
TopContributions: []models.ContributionData{
{
TotalContribution: util.RandomViewerCount(),
TotalContribution: util.RandomInt(10 * 100),
TypeOfContribution: util.RandomType(),
UserWhoMadeContribution: util.RandomUserID(),
UserNameWhoMadeContribution: "cli_user1",
Expand All @@ -94,7 +95,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
},
}
if triggerMapping[params.Transport][params.Trigger] == "hype-train-end " {
body.Event.CooldownEndsAtTimestamp = util.GetTimestamp().Format(time.RFC3339Nano)
body.Event.CooldownEndsAtTimestamp = util.GetTimestamp().Add(1 * time.Hour).Format(time.RFC3339Nano)
}
event, err = json.Marshal(body)
if err != nil {
Expand All @@ -119,7 +120,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
TypeOfContribution: lastType,
WebSubUser: lastUser,
},
Level: util.RandomViewerCount() % 4,
Level: util.RandomInt(4) + 1,
StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339),
TopContributions: []models.ContributionData{
{
Expand All @@ -128,7 +129,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
WebSubUser: lastUser,
},
{
TotalContribution: util.RandomViewerCount(),
TotalContribution: util.RandomInt(10 * 100),
TypeOfContribution: util.RandomType(),
WebSubUser: util.RandomUserID(),
},
Expand Down
21 changes: 15 additions & 6 deletions internal/util/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,34 @@ func RandomClientID() string {
return fmt.Sprintf("%x", b)[:30]
}

// RandomViewerCount generates a fake viewercount between 0->10,000,000
// RandomViewerCount generates a fake viewercount between 0->100,000
func RandomViewerCount() int64 {
viewer, err := rand.Int(rand.Reader, big.NewInt(1*10*100*100*100))
viewer, err := rand.Int(rand.Reader, big.NewInt(10*100*100))
if err != nil {
log.Fatal(err.Error())
}
return viewer.Int64()
}

//RandomInt generates a random integer between 0->max
func RandomInt(max int64) int64 {
someInt, err := rand.Int(rand.Reader, big.NewInt(max))
if err != nil {
log.Fatal(err.Error())
}

return someInt.Int64()
}

// RandomType generates a fake type; Either bits or subscription, in roughly even distribution
func RandomType() string {
someInt, err := rand.Int(rand.Reader, big.NewInt(1*10*100*100*100))
if err != nil {
log.Fatal(err.Error())
}
if (someInt.Int64()%2) == 0{
if (someInt.Int64() % 2) == 0 {
return "bits"
}else
{
} else {
return "subscription"
}
}
}
19 changes: 19 additions & 0 deletions internal/util/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,22 @@ func TestRandomViewerCount(t *testing.T) {

a.NotEmpty(viewers)
}

func TestRandomType(t *testing.T) {
a := assert.New(t)

// run the test 20 times to make sure to get at least one of each random type
for i := 0; i < 20; i++ {
randomType := RandomType()

a.NotEmpty(randomType)
}
}

func TestRandomInt(t *testing.T) {
a := assert.New(t)

randomInt := RandomInt(10)

a.Equal(true, randomInt >= 0)
}

0 comments on commit 4dc7c54

Please sign in to comment.