Skip to content

Commit 4dc7c54

Browse files
committed
Adding new RandomInt utility + updating tests
1 parent 9dcc2c8 commit 4dc7c54

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
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 {
@@ -63,12 +64,12 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
6364
BroadcasterUserID: params.ToUserID,
6465
BroadcasterUserLogin: params.ToUserName,
6566
BroadcasterUserName: params.ToUserName,
66-
Total: util.RandomViewerCount(),
67+
Total: localTotal,
6768
Progress: localProgress,
6869
Goal: localGoal,
6970
TopContributions: []models.ContributionData{
7071
{
71-
TotalContribution: util.RandomViewerCount(),
72+
TotalContribution: util.RandomInt(10 * 100),
7273
TypeOfContribution: util.RandomType(),
7374
UserWhoMadeContribution: util.RandomUserID(),
7475
UserNameWhoMadeContribution: "cli_user1",
@@ -94,7 +95,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
9495
},
9596
}
9697
if triggerMapping[params.Transport][params.Trigger] == "hype-train-end " {
97-
body.Event.CooldownEndsAtTimestamp = util.GetTimestamp().Format(time.RFC3339Nano)
98+
body.Event.CooldownEndsAtTimestamp = util.GetTimestamp().Add(1 * time.Hour).Format(time.RFC3339Nano)
9899
}
99100
event, err = json.Marshal(body)
100101
if err != nil {
@@ -119,7 +120,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
119120
TypeOfContribution: lastType,
120121
WebSubUser: lastUser,
121122
},
122-
Level: util.RandomViewerCount() % 4,
123+
Level: util.RandomInt(4) + 1,
123124
StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339),
124125
TopContributions: []models.ContributionData{
125126
{
@@ -128,7 +129,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
128129
WebSubUser: lastUser,
129130
},
130131
{
131-
TotalContribution: util.RandomViewerCount(),
132+
TotalContribution: util.RandomInt(10 * 100),
132133
TypeOfContribution: util.RandomType(),
133134
WebSubUser: util.RandomUserID(),
134135
},

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)