Skip to content

Commit

Permalink
added task for setCurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel rivera authored and daniel rivera committed Apr 29, 2024
1 parent e8b2381 commit dc57ce2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ COPY go.sum .

# Copy the application source code to the container

COPY /common/* ./common/
COPY /utils/* ./utils/
COPY /internal/worker/* ./internal/worker/
COPY /cmd/load_generator.go ./load_generator.go

Expand Down
33 changes: 32 additions & 1 deletion cmd/load_generator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"devoteam-load-generator/common"
"devoteam-load-generator/internal/worker"
"devoteam-load-generator/utils"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -30,6 +32,28 @@ func index() {
}
}

func setCurrency() {

// create http worker
serCurrencyWorker := worker.HttpWorker{
Url: "http://192.168.0.100:8080/setCurrency",
HttpMethod: "POST",
HttpClient: client,
ContentType: "application/x-www-form-urlencoded",
Body: map[string]string{
"currency_code": utils.PickupRandom(common.Currencies),
},
}

outputHttpWorker, err := serCurrencyWorker.Run()

if err != nil {
globalBoomer.RecordFailure(serCurrencyWorker.Url, err.Error(), outputHttpWorker.ElapsedTime, err.Error())
} else {
globalBoomer.RecordSuccess(serCurrencyWorker.Url, strconv.Itoa(outputHttpWorker.StatusCode), outputHttpWorker.ElapsedTime, outputHttpWorker.LenghtBody)
}
}

var globalBoomer *boomer.Boomer

func main() {
Expand All @@ -45,11 +69,18 @@ func main() {
Fn: index,
}

task2 := &boomer.Task{
Name: "setCurrency",
Weight: 5,
Fn: setCurrency,
}

ts.AddTask(task1)
ts.AddTask(task2)

numClients := 2
spawnRate := float64(10)
globalBoomer = boomer.NewStandaloneBoomer(numClients, spawnRate)
globalBoomer.AddOutput(boomer.NewConsoleOutput())
globalBoomer.Run(task1)
globalBoomer.Run(task1, task2)
}
56 changes: 47 additions & 9 deletions cmd/load_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/gin-gonic/gin"
)

func TestWorker(t *testing.T) {
func TestWorkerGet(t *testing.T) {
// Define expected response data
expectedData := worker.OutputHttpWorker{
StatusCode: 200,
Expand Down Expand Up @@ -58,24 +58,62 @@ func TestWorker(t *testing.T) {
HttpClient: client,
}

testPostWorker := worker.HttpWorker{
Url: ts.URL,
HttpMethod: "POST",
HttpClient: client,
}
// Update Worker function to use the test server URL
_, err = testGetWorker.Run()

_, err2 := testPostWorker.Run()

// Assertions
// compare if error is different to nil or ERROR

if err != nil && err != common.ErrorTimeout {
t.Errorf("Unexpected error: %v", err)
}

if err2 != nil && err2 != common.ErrorTimeout {
}

func TestWorkerPost(t *testing.T) {
// Define expected response data
expectedData := worker.OutputHttpWorker{
StatusCode: 200,
}
//expectedData := []byte(`{"message": "success"}`)
jsonData, err := json.Marshal(expectedData)

fmt.Println(jsonData)

if err != nil {
t.Errorf("Error marshalling mock response: %v", err)
return
}
// Create a Gin router for mocking
ginRouter := gin.Default()

ginRouter.POST("/cart", func(c *gin.Context) {

c.Writer.Header().Set("Content-Type", "application/json")
c.Writer.Write(jsonData)
c.Writer.WriteHeader(http.StatusOK)

})

// Create a server using the Gin router
ts2 := httptest.NewServer(ginRouter)
defer ts2.Close()

// define HttpWorker
client := &http.Client{}

testPostWorker := worker.HttpWorker{
Url: ts2.URL,
HttpMethod: "POST",
HttpClient: client,
}
// Update Worker function to use the test server URL
_, err = testPostWorker.Run()

// Assertions
// compare if error is different to nil or ERROR

if err != nil && err != common.ErrorTimeout {
t.Errorf("Unexpected error: %v", err)
}

Expand Down
17 changes: 17 additions & 0 deletions common/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package common

// Common errors
var (
Currencies = []string{"EUR", "USD", "JPY", "CAD", "GBP", "TRY"}
Products = []string{
"0PUK6V6EV0",
"1YMWWN1N4O",
"2ZYFJ3GM2N",
"66VCHSJNUP",
"6E92ZMYYFZ",
"9SIQT8TOJO",
"L9ECAV7KIM",
"LS4PSXUNUM",
"OLJCESPC7Z",
}
)
4 changes: 2 additions & 2 deletions internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ func (hw *HttpWorker) Run() (OutputHttpWorker, error) {

// Validate if error is Timeout or the site is not up
if err != nil {
if strings.Contains(err.Error(), "timeout") {
if strings.Contains(err.Error(), "Client.Timeout") {
//log.Println("Error creating request", err)
return output, common.ErrorTimeout
} else {
//log.Println("Error creating request", err)
log.Fatal("Worker ERROR, bad request ", err)
return output, err
}
}
Expand Down
7 changes: 7 additions & 0 deletions utils/pickup_random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package utils

import "math/rand"

func PickupRandom(list []string) string {
return list[rand.Intn(len(list))]
}

0 comments on commit dc57ce2

Please sign in to comment.