Skip to content

Commit 4296faa

Browse files
authored
Merge pull request #50 from twitchdev/bugfix/utility-updates
Adding new RandomInt utility + updating tests
2 parents c67cbc1 + 7cf9c1f commit 4296faa

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

internal/events/types/hype_train/hype_train_event.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
3535
var event []byte
3636
var err error
3737
lastUser := util.RandomUserID()
38-
lastTotal := util.RandomViewerCount()
38+
lastTotal := util.RandomInt(10 * 100)
3939
lastType := util.RandomType()
40+
4041
//Local variables which will be used for the trigger params below
41-
localTotal := util.RandomViewerCount()
42-
localGoal := util.RandomViewerCount()
42+
localTotal := util.RandomInt(10 * 100)
43+
localGoal := util.RandomInt(10*100*100) + localTotal
4344
localProgress := (localTotal / localGoal)
4445

4546
switch params.Transport {
@@ -64,12 +65,12 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
6465
BroadcasterUserID: params.ToUserID,
6566
BroadcasterUserLogin: params.ToUserName,
6667
BroadcasterUserName: params.ToUserName,
67-
Total: util.RandomViewerCount(),
68+
Total: localTotal,
6869
Progress: localProgress,
6970
Goal: localGoal,
7071
TopContributions: []models.ContributionData{
7172
{
72-
TotalContribution: util.RandomViewerCount(),
73+
TotalContribution: util.RandomInt(10 * 100),
7374
TypeOfContribution: util.RandomType(),
7475
UserWhoMadeContribution: util.RandomUserID(),
7576
UserNameWhoMadeContribution: "cli_user1",
@@ -95,7 +96,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
9596
},
9697
}
9798
if triggerMapping[params.Transport][params.Trigger] == "hype-train-end " {
98-
body.Event.CooldownEndsAtTimestamp = util.GetTimestamp().Format(time.RFC3339Nano)
99+
body.Event.CooldownEndsAtTimestamp = util.GetTimestamp().Add(1 * time.Hour).Format(time.RFC3339Nano)
99100
}
100101
event, err = json.Marshal(body)
101102
if err != nil {
@@ -120,7 +121,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
120121
TypeOfContribution: lastType,
121122
WebSubUser: lastUser,
122123
},
123-
Level: util.RandomViewerCount() % 4,
124+
Level: util.RandomInt(4) + 1,
124125
StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339),
125126
TopContributions: []models.ContributionData{
126127
{
@@ -129,7 +130,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
129130
WebSubUser: lastUser,
130131
},
131132
{
132-
TotalContribution: util.RandomViewerCount(),
133+
TotalContribution: util.RandomInt(10 * 100),
133134
TypeOfContribution: util.RandomType(),
134135
WebSubUser: util.RandomUserID(),
135136
},

internal/events/types/streamup/streamup.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
7979
GameID: "509658",
8080
Type: "live",
8181
Title: params.StreamTitle,
82-
ViewerCount: 1337,
82+
ViewerCount: util.RandomViewerCount(),
8383
StartedAt: util.GetTimestamp().Format(time.RFC3339),
8484
Language: "en",
8585
ThumbnailURL: "https://static-cdn.jtvnw.net/ttv-static/404_preview-440x248.jpg",

internal/util/random.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,34 @@ func RandomClientID() string {
3838
return fmt.Sprintf("%x", b)[:30]
3939
}
4040

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

50+
//RandomInt generates a random integer between 0->max
51+
func RandomInt(max int64) int64 {
52+
someInt, err := rand.Int(rand.Reader, big.NewInt(max))
53+
if err != nil {
54+
log.Fatal(err.Error())
55+
}
56+
57+
return someInt.Int64()
58+
}
59+
5060
// RandomType generates a fake type; Either bits or subscription, in roughly even distribution
5161
func RandomType() string {
5262
someInt, err := rand.Int(rand.Reader, big.NewInt(1*10*100*100*100))
5363
if err != nil {
5464
log.Fatal(err.Error())
5565
}
56-
if (someInt.Int64()%2) == 0{
66+
if (someInt.Int64() % 2) == 0 {
5767
return "bits"
58-
}else
59-
{
68+
} else {
6069
return "subscription"
6170
}
62-
}
71+
}

internal/util/random_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,22 @@ func TestRandomViewerCount(t *testing.T) {
3737

3838
a.NotEmpty(viewers)
3939
}
40+
41+
func TestRandomType(t *testing.T) {
42+
a := assert.New(t)
43+
44+
// run the test 20 times to make sure to get at least one of each random type
45+
for i := 0; i < 20; i++ {
46+
randomType := RandomType()
47+
48+
a.NotEmpty(randomType)
49+
}
50+
}
51+
52+
func TestRandomInt(t *testing.T) {
53+
a := assert.New(t)
54+
55+
randomInt := RandomInt(10)
56+
57+
a.Equal(true, randomInt >= 0)
58+
}

0 commit comments

Comments
 (0)