Skip to content

Commit

Permalink
fix(strm-1003): exit upon errors with simulating events
Browse files Browse the repository at this point in the history
  • Loading branch information
trietsch committed Apr 25, 2022
1 parent 1ec7e18 commit 9e2247a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 45 deletions.
32 changes: 0 additions & 32 deletions pkg/simulator/legacy_schemas.go

This file was deleted.

10 changes: 3 additions & 7 deletions pkg/simulator/random_events/randomsim.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strmprivacy/strm/pkg/auth"
"strmprivacy/strm/pkg/common"
"strmprivacy/strm/pkg/entity/stream"
sim "strmprivacy/strm/pkg/simulator"
"strmprivacy/strm/pkg/simulator"
"strmprivacy/strm/pkg/util"
)

Expand Down Expand Up @@ -63,11 +63,7 @@ func run(cmd *cobra.Command, streamName *string) {

client := http.Client{}
var sender sim.Sender
if schema == "clickstream" {
sender = sim.LegacySender{Client: client, Gateway: gateway, Schema: schema}
} else {
sender = sim.ModernSender{Client: client, Gateway: gateway, Schema: schema}
}
sender = sim.ModernSender{Client: client, Gateway: gateway, Schema: schema}

var ct = 0
now := time.Now()
Expand All @@ -80,7 +76,7 @@ func run(cmd *cobra.Command, streamName *string) {
ct += 1
time.Sleep(interval * time.Millisecond)
if !quiet && time.Now().Sub(now) > 5*time.Second {
println("Sent", ct, "events")
//println("Sent", ct, "events")
now = time.Now()
}
}
Expand Down
18 changes: 12 additions & 6 deletions pkg/simulator/sims.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sim

import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -35,16 +36,21 @@ func (s ModernSender) Send(event StrmPrivacyEvent, token string) {
b := &bytes.Buffer{}
err := event.Serialize(b)
common.CliExit(err)

req, err := http.NewRequest("POST", s.Gateway, b)
common.CliExit(err)

req.Header.Set("Strm-Schema-Ref", s.Schema)
req.Header.Set("Authorization", "Bearer "+token)

resp, err := s.Client.Do(req)
if err != nil || resp.StatusCode != 204 {
if resp != nil {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
fmt.Printf("%v %s\n", err, string(body))
}

if err != nil {
common.CliExit(errors.New(fmt.Sprintf("An error occurred while simulating events.\nError: %v", err)))
} else if resp.StatusCode != 204 {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)

common.CliExit(errors.New(fmt.Sprintf("The simulated event sent resulted in an error (expected status is not 204).\nError: %v, Response Body: %s", err, string(body))))
}
}

0 comments on commit 9e2247a

Please sign in to comment.