Skip to content

Commit

Permalink
Merge pull request #50 from twitchdev/bugfix/utility-updates
Browse files Browse the repository at this point in the history
Adding new RandomInt utility + updating tests
  • Loading branch information
lleadbet authored Apr 16, 2021
2 parents c67cbc1 + 7cf9c1f commit 4296faa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 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 @@ -64,12 +65,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 @@ -95,7 +96,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 @@ -120,7 +121,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 @@ -129,7 +130,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
2 changes: 1 addition & 1 deletion internal/events/types/streamup/streamup.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
GameID: "509658",
Type: "live",
Title: params.StreamTitle,
ViewerCount: 1337,
ViewerCount: util.RandomViewerCount(),
StartedAt: util.GetTimestamp().Format(time.RFC3339),
Language: "en",
ThumbnailURL: "https://static-cdn.jtvnw.net/ttv-static/404_preview-440x248.jpg",
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 4296faa

Please sign in to comment.