-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement #602 and fix simple server #604
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ import ( | |
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
"net/http" | ||
"io/ioutil" | ||
"time" | ||
) | ||
|
||
func TestServerInitialization(t *testing.T) { | ||
|
@@ -28,6 +31,53 @@ func TestServerInitialization(t *testing.T) { | |
} | ||
} | ||
|
||
func TestServerSetupResponse(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expectedBody string | ||
expectedStatus int | ||
}{ | ||
{` | ||
require "net/simple_server" | ||
|
||
server = Net::SimpleServer.new(4000) | ||
server.get("/") do |req, res| | ||
res.body = req.method + " Hello World" | ||
res.status = 200 | ||
end | ||
puts("foo") | ||
server.start | ||
`, | ||
"GET Hello World", | ||
200}, | ||
} | ||
|
||
for _, tt := range tests { | ||
go func() { | ||
v := initTestVM() | ||
v.testEval(t, tt.input, getFilename()) | ||
}() | ||
|
||
time.Sleep(1 * time.Second) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is my only concern. Would calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean? It'll make the vm inside goroutine to run the server (and hang there). This sleep is just to make sure we don't make the request before the server is started There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I metioned the wrong syncing mechanism, meant a wait group. I just think we shouldn't rely on thread do
server.start
end the tests function waitgroup.Add(1)
go func() {
...
v.testEval(...) // does the vm die after this statement? or will the server still be running?
waitgroup.Done()
}
waitgroup.Wait()
// run the tests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ear7h The problem is that since we're running a web server in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah okay, I wasn't sure of the mechanics of the test vm |
||
resp, err := http.Get("http://localhost:4000") | ||
|
||
if err != nil { | ||
t.Fatal(err.Error()) | ||
} | ||
|
||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
|
||
if string(body) != tt.expectedBody { | ||
t.Fatalf("Expect response body to be: \n %s, got \n %s", tt.expectedBody, string(body)) | ||
} | ||
|
||
if resp.StatusCode != tt.expectedStatus { | ||
t.Fatalf("Expect response status to be %d, got %d", tt.expectedStatus, resp.StatusCode) | ||
} | ||
} | ||
} | ||
|
||
func TestSetupResponseDefaultValue(t *testing.T) { | ||
reader := strings.NewReader("") | ||
recorder := httptest.NewRecorder() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also grouped with the std lib
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ear7h I already ran
gofmt
and I want to follow what it changes/allowsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but I believe gofmt preserves newlines so the two groups of imports are formatted independently of each other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you insist I can change this time, but I don't see the point to group them all manually in every future PRs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, makes sense