Skip to content

Commit d79d6b5

Browse files
authored
add test support for port and env (#1395)
1 parent c2ab1eb commit d79d6b5

File tree

20 files changed

+143
-205
lines changed

20 files changed

+143
-205
lines changed

examples/grpc/grpc-server/main_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package main
22

33
import (
44
"context"
5-
"fmt"
6-
"gofr.dev/examples/grpc/grpc-server/server"
7-
"strconv"
85
"testing"
96
"time"
107

@@ -13,16 +10,13 @@ import (
1310
"google.golang.org/grpc"
1411
"google.golang.org/grpc/credentials/insecure"
1512

13+
"gofr.dev/examples/grpc/grpc-server/server"
1614
"gofr.dev/pkg/gofr/testutil"
1715
)
1816

1917
func TestGRPCServer(t *testing.T) {
20-
gRPCPort := testutil.GetFreePort(t)
21-
t.Setenv("GRPC_PORT", strconv.Itoa(gRPCPort))
22-
host := fmt.Sprint("localhost:", gRPCPort)
23-
24-
port := testutil.GetFreePort(t)
25-
t.Setenv("METRICS_PORT", strconv.Itoa(port))
18+
configs := testutil.NewServerConfigs(t)
19+
host := configs.GRPCHost
2620

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

examples/http-server-using-redis/main_test.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
"net/http"
8-
"strconv"
98
"testing"
109
"time"
1110

@@ -23,12 +22,7 @@ import (
2322
)
2423

2524
func TestHTTPServerUsingRedis(t *testing.T) {
26-
httpPort := testutil.GetFreePort(t)
27-
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
28-
host := fmt.Sprint("http://localhost:", httpPort)
29-
30-
port := testutil.GetFreePort(t)
31-
t.Setenv("METRICS_PORT", strconv.Itoa(port))
25+
configs := testutil.NewServerConfigs(t)
3226

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

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

6559
func TestRedisSetHandler(t *testing.T) {
66-
metricsPort := testutil.GetFreePort(t)
67-
httpPort := testutil.GetFreePort(t)
68-
69-
t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
70-
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
60+
configs := testutil.NewServerConfigs(t)
7161

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

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

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

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

9484
func TestRedisPipelineHandler(t *testing.T) {
95-
metricsPort := testutil.GetFreePort(t)
96-
httpPort := testutil.GetFreePort(t)
97-
98-
t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
99-
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
85+
configs := testutil.NewServerConfigs(t)
10086

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

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

114100
gofrReq := gofrHTTP.NewRequest(req)

examples/using-add-rest-handlers/main_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package main
22

33
import (
44
"bytes"
5-
"fmt"
65
"net/http"
7-
"strconv"
86
"testing"
97
"time"
108

@@ -15,12 +13,7 @@ import (
1513
)
1614

1715
func TestIntegration_AddRESTHandlers(t *testing.T) {
18-
httpPort := testutil.GetFreePort(t)
19-
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
20-
host := fmt.Sprint("http://localhost:", httpPort)
21-
22-
port := testutil.GetFreePort(t)
23-
t.Setenv("METRICS_PORT", strconv.Itoa(port))
16+
configs := testutil.NewServerConfigs(t)
2417

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

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

4942
c := http.Client{}

examples/using-cron-jobs/main_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package main
22

33
import (
4-
"strconv"
54
"testing"
65
"time"
76

87
"github.com/stretchr/testify/assert"
9-
108
"gofr.dev/pkg/gofr/testutil"
119
)
1210

1311
func Test_UserPurgeCron(t *testing.T) {
14-
port := testutil.GetFreePort(t)
15-
t.Setenv("METRICS_PORT", strconv.Itoa(port))
12+
configs := testutil.NewServerConfigs(t)
1613

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

2825
assert.Equal(t, expected, m)
26+
t.Logf("Metrics server running at: %s", configs.MetricsHost)
2927
}

examples/using-custom-metrics/main_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"io"
66
"net/http"
7-
"strconv"
87
"testing"
98
"time"
109

@@ -14,38 +13,33 @@ import (
1413
)
1514

1615
func TestIntegration(t *testing.T) {
17-
httpPort := testutil.GetFreePort(t)
18-
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
19-
host := fmt.Sprint("http://localhost:", httpPort)
20-
21-
metricsPort := testutil.GetFreePort(t)
22-
t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
16+
configs := testutil.NewServerConfigs(t)
2317

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

2721
c := http.Client{}
2822

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

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

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

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

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

4640
resp, err := c.Do(req)
4741
if err != nil {
48-
t.Fatalf("request to localhost:%d/metrics failed: %v", metricsPort, err)
42+
t.Fatalf("request to localhost:%d/metrics failed: %v", configs.MetricsPort, err)
4943
}
5044

5145
body, _ := io.ReadAll(resp.Body)

examples/using-file-bind/main_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package main
22

33
import (
44
"bytes"
5-
"fmt"
65
"io"
76
"mime/multipart"
87
"net/http"
98
"os"
10-
"strconv"
119
"testing"
1210
"time"
1311

@@ -18,27 +16,22 @@ import (
1816
)
1917

2018
func TestMain_BindError(t *testing.T) {
21-
httpPort := testutil.GetFreePort(t)
22-
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
23-
host := fmt.Sprint("http://localhost:", httpPort)
24-
25-
port := testutil.GetFreePort(t)
26-
t.Setenv("METRICS_PORT", strconv.Itoa(port))
19+
configs := testutil.NewServerConfigs(t)
2720

2821
go main()
2922
time.Sleep(100 * time.Millisecond)
3023

3124
c := http.Client{}
3225

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

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

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

examples/using-http-service/main_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io"
88
"net/http"
99
"net/http/httptest"
10-
"strconv"
1110
"testing"
1211
"time"
1312

@@ -25,12 +24,7 @@ import (
2524
var port int
2625

2726
func Test_main(t *testing.T) {
28-
port = testutil.GetFreePort(t)
29-
t.Setenv("HTTP_PORT", strconv.Itoa(port))
30-
host := fmt.Sprint("http://localhost:", port)
31-
32-
metricsPort := testutil.GetFreePort(t)
33-
t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
27+
configs := testutil.NewServerConfigs(t)
3428

3529
c := &http.Client{}
3630

@@ -60,7 +54,7 @@ func Test_main(t *testing.T) {
6054
}
6155

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

6660
require.NoError(t, err, "TEST[%d], Failed.\n%s", i, tc.desc)

examples/using-migrations/main_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package main
22

33
import (
44
"bytes"
5-
"fmt"
65
"net/http"
7-
"strconv"
86
"testing"
97
"time"
108

@@ -15,12 +13,7 @@ import (
1513
)
1614

1715
func TestExampleMigration(t *testing.T) {
18-
httpPort := testutil.GetFreePort(t)
19-
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
20-
host := fmt.Sprint("http://localhost:", httpPort)
21-
22-
port := testutil.GetFreePort(t)
23-
t.Setenv("METRICS_PORT", strconv.Itoa(port))
16+
configs := testutil.NewServerConfigs(t)
2417

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

4639
for i, tc := range tests {
47-
req, _ := http.NewRequest(tc.method, host+tc.path, bytes.NewBuffer(tc.body))
40+
req, _ := http.NewRequest(tc.method, configs.HTTPHost+tc.path, bytes.NewBuffer(tc.body))
4841
req.Header.Set("content-type", "application/json")
4942
c := http.Client{}
5043
resp, err := c.Do(req)

examples/using-publisher/main_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"net/http"
7-
"strconv"
87
"testing"
98
"time"
109

@@ -15,12 +14,7 @@ import (
1514
)
1615

1716
func TestExamplePublisher(t *testing.T) {
18-
httpPort := testutil.GetFreePort(t)
19-
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
20-
host := fmt.Sprint("http://localhost:", httpPort)
21-
22-
port := testutil.GetFreePort(t)
23-
t.Setenv("METRICS_PORT", strconv.Itoa(port))
17+
configs := testutil.NewServerConfigs(t)
2418

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

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

77-
httpPort := testutil.GetFreePort(t)
78-
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
79-
80-
metricsPort := testutil.GetFreePort(t)
81-
t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
71+
configs := testutil.NewServerConfigs(t)
8272

83-
host := fmt.Sprint("http://localhost:", httpPort)
73+
host := fmt.Sprint("http://localhost:", configs.HTTPPort)
8474

8575
go main()
8676
time.Sleep(200 * time.Millisecond)

examples/using-subscriber/main_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"strconv"
65
"strings"
76
"testing"
87
"time"
@@ -41,11 +40,7 @@ func initializeTest(t *testing.T) {
4140

4241
func TestExampleSubscriber(t *testing.T) {
4342
log := testutil.StdoutOutputForFunc(func() {
44-
httpPort := testutil.GetFreePort(t)
45-
t.Setenv("HTTP_PORT", strconv.Itoa(httpPort))
46-
47-
metricsPort := testutil.GetFreePort(t)
48-
t.Setenv("METRICS_PORT", strconv.Itoa(metricsPort))
43+
testutil.NewServerConfigs(t)
4944

5045
go main()
5146
time.Sleep(time.Second * 1) // Giving some time to start the server

0 commit comments

Comments
 (0)