mirror of
				https://github.com/NotAShelf/tct.git
				synced 2025-10-31 19:32:37 +00:00 
			
		
		
		
	unit tests
This commit is contained in:
		
					parent
					
						
							
								4809f916cd
							
						
					
				
			
			
				commit
				
					
						a0955a4f6b
					
				
			
		
					 4 changed files with 95 additions and 3 deletions
				
			
		
							
								
								
									
										9
									
								
								go.mod
									
										
									
									
									
								
							
							
						
						
									
										9
									
								
								go.mod
									
										
									
									
									
								
							|  | @ -1,3 +1,12 @@ | |||
| module notashelf.dev/tct | ||||
| 
 | ||||
| go 1.22.2 | ||||
| 
 | ||||
| require github.com/stretchr/testify v1.9.0 | ||||
| 
 | ||||
| require ( | ||||
| 	github.com/davecgh/go-spew v1.1.1 // indirect | ||||
| 	github.com/pmezard/go-difflib v1.0.0 // indirect | ||||
| 	github.com/stretchr/objx v0.5.2 // indirect | ||||
| 	gopkg.in/yaml.v3 v3.0.1 // indirect | ||||
| ) | ||||
|  |  | |||
							
								
								
									
										12
									
								
								go.sum
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								go.sum
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,12 @@ | |||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||||
| github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= | ||||
| github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= | ||||
| github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||||
| github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||||
| gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||||
| gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||||
							
								
								
									
										15
									
								
								main.go
									
										
									
									
									
								
							
							
						
						
									
										15
									
								
								main.go
									
										
									
									
									
								
							|  | @ -4,6 +4,7 @@ import ( | |||
| 	"context" | ||||
| 	"flag" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"net/http" | ||||
| 	"os" | ||||
| 	"os/signal" | ||||
|  | @ -12,6 +13,12 @@ import ( | |||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| type HttpClient interface { | ||||
| 	Get(url string) (*http.Response, error) | ||||
| } | ||||
| 
 | ||||
| var httpClient HttpClient = &http.Client{} | ||||
| 
 | ||||
| func main() { | ||||
| 	urlPtr := flag.String("url", "http://example.com", "URL to fetch") | ||||
| 	maxRequestsPtr := flag.Int("max", 100, "Maximum number of parallel requests") | ||||
|  | @ -45,7 +52,7 @@ func main() { | |||
| 					return | ||||
| 				default: | ||||
| 					time.Sleep(delay) | ||||
| 					makeRequest(url) | ||||
| 					makeRequest(httpClient, url) | ||||
| 				} | ||||
| 			}() | ||||
| 		} | ||||
|  | @ -60,11 +67,13 @@ func main() { | |||
| 	fmt.Printf("\nOptimal Number of Parallel TCP Requests: %d\n", optimal) | ||||
| } | ||||
| 
 | ||||
| func makeRequest(url string) { | ||||
| 	resp, err := http.Get(url) | ||||
| func makeRequest(client HttpClient, url string) { | ||||
| 	resp, err := client.Get(url) | ||||
| 	if err != nil { | ||||
| 		fmt.Println(err) | ||||
| 		return | ||||
| 	} | ||||
| 	defer resp.Body.Close() | ||||
| 	body, _ := io.ReadAll(resp.Body) | ||||
| 	_ = body | ||||
| } | ||||
|  |  | |||
							
								
								
									
										62
									
								
								main_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								main_test.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,62 @@ | |||
| package main | ||||
| 
 | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"errors" | ||||
| 	"io" | ||||
| 	"net/http" | ||||
| 	"net/http/httptest" | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"github.com/stretchr/testify/mock" | ||||
| ) | ||||
| 
 | ||||
| type MockHttpClient struct { | ||||
| 	mock.Mock | ||||
| } | ||||
| 
 | ||||
| func (m *MockHttpClient) Get(url string) (*http.Response, error) { | ||||
| 	args := m.Called(url) | ||||
| 	return args.Get(0).(*http.Response), args.Error(1) | ||||
| } | ||||
| 
 | ||||
| func TestMakeRequest(t *testing.T) { | ||||
| 	tests := []struct { | ||||
| 		name          string | ||||
| 		statusCode    int | ||||
| 		responseBody  string | ||||
| 		expectedError error | ||||
| 	}{ | ||||
| 		{ | ||||
| 			name:         "Successful request", | ||||
| 			statusCode:   http.StatusOK, | ||||
| 			responseBody: "OK", | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:          "Failed request", | ||||
| 			statusCode:    http.StatusInternalServerError, | ||||
| 			responseBody:  "", | ||||
| 			expectedError: errors.New("request failed"), | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	for _, tt := range tests { | ||||
| 		t.Run(tt.name, func(t *testing.T) { | ||||
| 			mockClient := new(MockHttpClient) | ||||
| 			server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 				w.WriteHeader(tt.statusCode) | ||||
| 				w.Write([]byte(tt.responseBody)) | ||||
| 			})) | ||||
| 			defer server.Close() | ||||
| 
 | ||||
| 			mockClient.On("Get", server.URL).Return(&http.Response{ | ||||
| 				StatusCode: tt.statusCode, | ||||
| 				Body:       io.NopCloser(bytes.NewBufferString(tt.responseBody)), | ||||
| 			}, tt.expectedError) | ||||
| 
 | ||||
| 			makeRequest(mockClient, server.URL) | ||||
| 
 | ||||
| 			mockClient.AssertExpectations(t) | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue