Skip to content

Commit

Permalink
add test support for port and env (#1395)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaidehiadhi authored Jan 23, 2025
1 parent c2ab1eb commit d79d6b5
Show file tree
Hide file tree
Showing 20 changed files with 143 additions and 205 deletions.
12 changes: 3 additions & 9 deletions examples/grpc/grpc-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package main

import (
"context"
"fmt"
"gofr.dev/examples/grpc/grpc-server/server"
"strconv"
"testing"
"time"

Expand All @@ -13,16 +10,13 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"gofr.dev/examples/grpc/grpc-server/server"
"gofr.dev/pkg/gofr/testutil"
)

func TestGRPCServer(t *testing.T) {
gRPCPort := testutil.GetFreePort(t)
t.Setenv("GRPC_PORT", strconv.Itoa(gRPCPort))
host := fmt.Sprint("localhost:", gRPCPort)

port := testutil.GetFreePort(t)
t.Setenv("METRICS_PORT", strconv.Itoa(port))
configs := testutil.NewServerConfigs(t)
host := configs.GRPCHost

go main()
time.Sleep(100 * time.Millisecond)
Expand Down
26 changes: 6 additions & 20 deletions examples/http-server-using-redis/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"net/http"
"strconv"
"testing"
"time"

Expand All @@ -23,12 +22,7 @@ import (
)

func TestHTTPServerUsingRedis(t *testing.T) {
httpPort := testutil.GetFreePort(t)
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
host := fmt.Sprint("http://localhost:", httpPort)

port := testutil.GetFreePort(t)
t.Setenv("METRICS_PORT", strconv.Itoa(port))
configs := testutil.NewServerConfigs(t)

go main()
time.Sleep(100 * time.Millisecond) // Giving some time to start the server
Expand All @@ -51,7 +45,7 @@ func TestHTTPServerUsingRedis(t *testing.T) {
}

for i, tc := range tests {
req, _ := http.NewRequest(tc.method, host+tc.path, bytes.NewBuffer(tc.body))
req, _ := http.NewRequest(tc.method, configs.HTTPHost+tc.path, bytes.NewBuffer(tc.body))
req.Header.Set("content-type", "application/json")
c := http.Client{}
resp, err := c.Do(req)
Expand All @@ -63,11 +57,7 @@ func TestHTTPServerUsingRedis(t *testing.T) {
}

func TestRedisSetHandler(t *testing.T) {
metricsPort := testutil.GetFreePort(t)
httpPort := testutil.GetFreePort(t)

t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
configs := testutil.NewServerConfigs(t)

a := gofr.New()
logger := logging.NewLogger(logging.DEBUG)
Expand All @@ -78,7 +68,7 @@ func TestRedisSetHandler(t *testing.T) {

mock.ExpectSet("key", "value", 5*time.Minute).SetErr(testutil.CustomError{ErrorMessage: "redis get error"})

req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, fmt.Sprintf("http://localhost:%d/handle", httpPort), bytes.NewBuffer([]byte(`{"key":"value"}`)))
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, fmt.Sprintf("http://localhost:%d/handle", configs.HTTPPort), bytes.NewBuffer([]byte(`{"key":"value"}`)))
req.Header.Set("content-type", "application/json")
gofrReq := gofrHTTP.NewRequest(req)

Expand All @@ -92,11 +82,7 @@ func TestRedisSetHandler(t *testing.T) {
}

func TestRedisPipelineHandler(t *testing.T) {
metricsPort := testutil.GetFreePort(t)
httpPort := testutil.GetFreePort(t)

t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
configs := testutil.NewServerConfigs(t)

a := gofr.New()
logger := logging.NewLogger(logging.DEBUG)
Expand All @@ -108,7 +94,7 @@ func TestRedisPipelineHandler(t *testing.T) {
mock.ExpectSet("testKey1", "testValue1", time.Minute*5).SetErr(testutil.CustomError{ErrorMessage: "redis get error"})
mock.ClearExpect()

req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, fmt.Sprint("http://localhost:", httpPort, "/handle"), bytes.NewBuffer([]byte(`{"key":"value"}`)))
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, fmt.Sprint("http://localhost:", configs.HTTPHost, "/handle"), bytes.NewBuffer([]byte(`{"key":"value"}`)))
req.Header.Set("content-type", "application/json")

gofrReq := gofrHTTP.NewRequest(req)
Expand Down
11 changes: 2 additions & 9 deletions examples/using-add-rest-handlers/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"bytes"
"fmt"
"net/http"
"strconv"
"testing"
"time"

Expand All @@ -15,12 +13,7 @@ import (
)

func TestIntegration_AddRESTHandlers(t *testing.T) {
httpPort := testutil.GetFreePort(t)
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
host := fmt.Sprint("http://localhost:", httpPort)

port := testutil.GetFreePort(t)
t.Setenv("METRICS_PORT", strconv.Itoa(port))
configs := testutil.NewServerConfigs(t)

go main()
time.Sleep(100 * time.Millisecond) // Giving some time to start the server
Expand All @@ -43,7 +36,7 @@ func TestIntegration_AddRESTHandlers(t *testing.T) {
}

for i, tc := range tests {
req, _ := http.NewRequest(tc.method, host+tc.path, bytes.NewReader(tc.body))
req, _ := http.NewRequest(tc.method, configs.HTTPHost+tc.path, bytes.NewReader(tc.body))
req.Header.Set("content-type", "application/json")

c := http.Client{}
Expand Down
6 changes: 2 additions & 4 deletions examples/using-cron-jobs/main_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package main

import (
"strconv"
"testing"
"time"

"github.com/stretchr/testify/assert"

"gofr.dev/pkg/gofr/testutil"
)

func Test_UserPurgeCron(t *testing.T) {
port := testutil.GetFreePort(t)
t.Setenv("METRICS_PORT", strconv.Itoa(port))
configs := testutil.NewServerConfigs(t)

go main()
time.Sleep(1100 * time.Millisecond)
Expand All @@ -26,4 +23,5 @@ func Test_UserPurgeCron(t *testing.T) {
mu.Unlock()

assert.Equal(t, expected, m)
t.Logf("Metrics server running at: %s", configs.MetricsHost)
}
16 changes: 5 additions & 11 deletions examples/using-custom-metrics/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"testing"
"time"

Expand All @@ -14,38 +13,33 @@ import (
)

func TestIntegration(t *testing.T) {
httpPort := testutil.GetFreePort(t)
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
host := fmt.Sprint("http://localhost:", httpPort)

metricsPort := testutil.GetFreePort(t)
t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
configs := testutil.NewServerConfigs(t)

go main()
time.Sleep(100 * time.Millisecond) // Giving some time to start the server

c := http.Client{}

req, _ := http.NewRequest(http.MethodPost, host+"/transaction", nil)
req, _ := http.NewRequest(http.MethodPost, configs.HTTPHost+"/transaction", nil)
req.Header.Set("content-type", "application/json")

_, err := c.Do(req)
if err != nil {
t.Fatalf("request to /transaction failed %v", err)
}

req, _ = http.NewRequest(http.MethodPost, host+"/return", nil)
req, _ = http.NewRequest(http.MethodPost, configs.HTTPHost+"/return", nil)

_, err = c.Do(req)
if err != nil {
t.Fatalf("request to /transaction failed %v", err)
}

req, _ = http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost:%d/metrics", metricsPort), nil)
req, _ = http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost:%d/metrics", configs.MetricsPort), nil)

resp, err := c.Do(req)
if err != nil {
t.Fatalf("request to localhost:%d/metrics failed: %v", metricsPort, err)
t.Fatalf("request to localhost:%d/metrics failed: %v", configs.MetricsPort, err)
}

body, _ := io.ReadAll(resp.Body)
Expand Down
13 changes: 3 additions & 10 deletions examples/using-file-bind/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package main

import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"strconv"
"testing"
"time"

Expand All @@ -18,27 +16,22 @@ import (
)

func TestMain_BindError(t *testing.T) {
httpPort := testutil.GetFreePort(t)
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
host := fmt.Sprint("http://localhost:", httpPort)

port := testutil.GetFreePort(t)
t.Setenv("METRICS_PORT", strconv.Itoa(port))
configs := testutil.NewServerConfigs(t)

go main()
time.Sleep(100 * time.Millisecond)

c := http.Client{}

req, _ := http.NewRequest(http.MethodPost, host+"/upload", http.NoBody)
req, _ := http.NewRequest(http.MethodPost, configs.HTTPHost+"/upload", http.NoBody)
req.Header.Set("content-type", "multipart/form-data")
resp, err := c.Do(req)

assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
require.NoError(t, err)

buf, contentType := generateMultiPartBody(t)
req, _ = http.NewRequest(http.MethodPost, host+"/upload", buf)
req, _ = http.NewRequest(http.MethodPost, configs.HTTPHost+"/upload", buf)
req.Header.Set("content-type", contentType)
req.ContentLength = int64(buf.Len())

Expand Down
10 changes: 2 additions & 8 deletions examples/using-http-service/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"

Expand All @@ -25,12 +24,7 @@ import (
var port int

func Test_main(t *testing.T) {
port = testutil.GetFreePort(t)
t.Setenv("HTTP_PORT", strconv.Itoa(port))
host := fmt.Sprint("http://localhost:", port)

metricsPort := testutil.GetFreePort(t)
t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
configs := testutil.NewServerConfigs(t)

c := &http.Client{}

Expand Down Expand Up @@ -60,7 +54,7 @@ func Test_main(t *testing.T) {
}

for i, tc := range testCases {
req, _ := http.NewRequest(http.MethodGet, host+tc.path, nil)
req, _ := http.NewRequest(http.MethodGet, configs.HTTPHost+tc.path, nil)
resp, err := c.Do(req)

require.NoError(t, err, "TEST[%d], Failed.\n%s", i, tc.desc)
Expand Down
11 changes: 2 additions & 9 deletions examples/using-migrations/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"bytes"
"fmt"
"net/http"
"strconv"
"testing"
"time"

Expand All @@ -15,12 +13,7 @@ import (
)

func TestExampleMigration(t *testing.T) {
httpPort := testutil.GetFreePort(t)
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
host := fmt.Sprint("http://localhost:", httpPort)

port := testutil.GetFreePort(t)
t.Setenv("METRICS_PORT", strconv.Itoa(port))
configs := testutil.NewServerConfigs(t)

go main()
time.Sleep(100 * time.Millisecond) // Giving some time to start the server
Expand All @@ -44,7 +37,7 @@ func TestExampleMigration(t *testing.T) {
}

for i, tc := range tests {
req, _ := http.NewRequest(tc.method, host+tc.path, bytes.NewBuffer(tc.body))
req, _ := http.NewRequest(tc.method, configs.HTTPHost+tc.path, bytes.NewBuffer(tc.body))
req.Header.Set("content-type", "application/json")
c := http.Client{}
resp, err := c.Do(req)
Expand Down
18 changes: 4 additions & 14 deletions examples/using-publisher/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"net/http"
"strconv"
"testing"
"time"

Expand All @@ -15,12 +14,7 @@ import (
)

func TestExamplePublisher(t *testing.T) {
httpPort := testutil.GetFreePort(t)
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
host := fmt.Sprint("http://localhost:", httpPort)

port := testutil.GetFreePort(t)
t.Setenv("METRICS_PORT", strconv.Itoa(port))
configs := testutil.NewServerConfigs(t)

go main()
time.Sleep(200 * time.Millisecond)
Expand Down Expand Up @@ -61,7 +55,7 @@ func TestExamplePublisher(t *testing.T) {
c := http.Client{}

for i, tc := range testCases {
req, _ := http.NewRequest(http.MethodPost, host+tc.path, bytes.NewBuffer(tc.body))
req, _ := http.NewRequest(http.MethodPost, configs.HTTPHost+tc.path, bytes.NewBuffer(tc.body))
req.Header.Set("content-type", "application/json")
resp, err := c.Do(req)
defer resp.Body.Close()
Expand All @@ -74,13 +68,9 @@ func TestExamplePublisher(t *testing.T) {
func TestExamplePublisherError(t *testing.T) {
t.Setenv("PUBSUB_BROKER", "localhost:1012")

httpPort := testutil.GetFreePort(t)
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))

metricsPort := testutil.GetFreePort(t)
t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
configs := testutil.NewServerConfigs(t)

host := fmt.Sprint("http://localhost:", httpPort)
host := fmt.Sprint("http://localhost:", configs.HTTPPort)

go main()
time.Sleep(200 * time.Millisecond)
Expand Down
7 changes: 1 addition & 6 deletions examples/using-subscriber/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -41,11 +40,7 @@ func initializeTest(t *testing.T) {

func TestExampleSubscriber(t *testing.T) {
log := testutil.StdoutOutputForFunc(func() {
httpPort := testutil.GetFreePort(t)
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))

metricsPort := testutil.GetFreePort(t)
t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
testutil.NewServerConfigs(t)

go main()
time.Sleep(time.Second * 1) // Giving some time to start the server
Expand Down
Loading

0 comments on commit d79d6b5

Please sign in to comment.