Skip to content

Commit

Permalink
use mojo test runnner
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Grenier <grenierb96@gmail.com>
  • Loading branch information
bgreni committed Sep 26, 2024
1 parent e695225 commit 6214c13
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ jobs:
run: |
curl -ssL https://magic.modular.com | bash
source $HOME/.bash_profile
magic run mojo run_tests.mojo
magic run test
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ install_id
.magic

# Rattler
output
output

# misc
.vscode
46 changes: 0 additions & 46 deletions bench.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ from lightbug_http.header import Headers, Header
from lightbug_http.utils import ByteReader, ByteWriter
from lightbug_http.http import HTTPRequest, HTTPResponse, encode
from lightbug_http.uri import URI
from tests.utils import (
TestStruct,
FakeResponder,
new_fake_listener,
FakeServer,
)

alias headers = bytes(
"""GET /index.html HTTP/1.1\r\nHost: example.com\r\nUser-Agent: Mozilla/5.0\r\nContent-Type: text/html\r\nContent-Length: 1234\r\nConnection: close\r\nTrailer: end-of-message\r\n\r\n"""
Expand Down Expand Up @@ -148,43 +142,3 @@ fn lightbug_benchmark_header_parse(inout b: Bencher):

b.iter[header_parse]()


fn lightbug_benchmark_server():
var server_report = benchmark.run[run_fake_server](max_iters=1)
print("Server: ")
server_report.print(benchmark.Unit.ms)


fn lightbug_benchmark_misc() -> None:
var direct_set_report = benchmark.run[init_test_and_set_a_direct](
max_iters=1
)

var recreating_set_report = benchmark.run[init_test_and_set_a_copy](
max_iters=1
)

print("Direct set: ")
direct_set_report.print(benchmark.Unit.ms)
print("Recreating set: ")
recreating_set_report.print(benchmark.Unit.ms)


var GetRequest = HTTPRequest(URI.parse("http://127.0.0.1/path")[URI])


fn run_fake_server():
var handler = FakeResponder()
var listener = new_fake_listener(2, encode(GetRequest))
var server = FakeServer(listener, handler)
server.serve()


fn init_test_and_set_a_copy() -> None:
var test = TestStruct("a", "b")
_ = test.set_a_copy("c")


fn init_test_and_set_a_direct() -> None:
var test = TestStruct("a", "b")
_ = test.set_a_direct("c")
2 changes: 1 addition & 1 deletion mojoproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = "0.1.4"
[tasks]
build = { cmd = "rattler-build build --recipe recipes -c https://conda.modular.com/max -c conda-forge --skip-existing=all", env = {MODULAR_MOJO_IMPORT_PATH = "$CONDA_PREFIX/lib/mojo"} }
publish = { cmd = "bash scripts/publish.sh", env = { PREFIX_API_KEY = "$PREFIX_API_KEY" } }
test = { cmd = "magic run mojo run_tests.mojo" }
test = { cmd = "magic run mojo test -I . tests" }
bench = { cmd = "magic run mojo bench.mojo" }
bench_server = { cmd = "magic run mojo build bench_server.mojo && ./bench_server ; rm bench_server" }
format = { cmd = "magic run mojo format -l 120 lightbug_http" }
Expand Down
13 changes: 0 additions & 13 deletions run_tests.mojo

This file was deleted.

Empty file removed tests/__init__.mojo
Empty file.
4 changes: 0 additions & 4 deletions tests/test_io.mojo → tests/lightbug_http/io/test_bytes.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ from collections import Dict, List
from lightbug_http.io.bytes import Bytes, bytes_equal, bytes


def test_io():
test_string_literal_to_bytes()


fn test_string_literal_to_bytes() raises:
var cases = Dict[StringLiteral, Bytes]()
cases[""] = Bytes()
Expand Down
41 changes: 12 additions & 29 deletions tests/test_client.mojo → tests/lightbug_http/sys/test_client.mojo
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
import testing
from tests.utils import (
default_server_conn_string,
)
from lightbug_http.sys.client import MojoClient
from lightbug_http.http import HTTPRequest, encode
from lightbug_http.uri import URI
from lightbug_http.header import Header, Headers
from lightbug_http.io.bytes import bytes


def test_client():
var mojo_client = MojoClient()
print("running 200 test")
test_mojo_client_lightbug_external_req_200(mojo_client)

print("running 301 test")
test_mojo_client_redirect_external_req_301(mojo_client)

# Seems like trying to run too many of these at once results in
# a 502 from httpbin

# print("running 302 test")
# test_mojo_client_redirect_external_req_302(mojo_client)
# print("running 307 test")
# test_mojo_client_redirect_external_req_307(mojo_client)
# print("running 308 test")
# test_mojo_client_redirect_external_req_308(mojo_client)
# test_mojo_client_redirect_external_req_google(mojo_client)


fn test_mojo_client_redirect_external_req_google(client: MojoClient) raises:
fn test_mojo_client_redirect_external_req_google() raises:
var client = MojoClient()
var req = HTTPRequest(
uri=URI.parse_raises("http://google.com"),
headers=Headers(
Expand All @@ -42,7 +20,8 @@ fn test_mojo_client_redirect_external_req_google(client: MojoClient) raises:
except e:
print(e)

fn test_mojo_client_redirect_external_req_302(client: MojoClient) raises:
fn test_mojo_client_redirect_external_req_302() raises:
var client = MojoClient()
var req = HTTPRequest(
uri=URI.parse_raises("http://httpbin.org/status/302"),
headers=Headers(
Expand All @@ -55,7 +34,8 @@ fn test_mojo_client_redirect_external_req_302(client: MojoClient) raises:
except e:
print(e)

fn test_mojo_client_redirect_external_req_308(client: MojoClient) raises:
fn test_mojo_client_redirect_external_req_308() raises:
var client = MojoClient()
var req = HTTPRequest(
uri=URI.parse_raises("http://httpbin.org/status/308"),
headers=Headers(
Expand All @@ -68,7 +48,8 @@ fn test_mojo_client_redirect_external_req_308(client: MojoClient) raises:
except e:
print(e)

fn test_mojo_client_redirect_external_req_307(client: MojoClient) raises:
fn test_mojo_client_redirect_external_req_307() raises:
var client = MojoClient()
var req = HTTPRequest(
uri=URI.parse_raises("http://httpbin.org/status/307"),
headers=Headers(
Expand All @@ -81,7 +62,8 @@ fn test_mojo_client_redirect_external_req_307(client: MojoClient) raises:
except e:
print(e)

fn test_mojo_client_redirect_external_req_301(client: MojoClient) raises:
fn test_mojo_client_redirect_external_req_301() raises:
var client = MojoClient()
var req = HTTPRequest(
uri=URI.parse_raises("http://httpbin.org/status/301"),
headers=Headers(
Expand All @@ -95,7 +77,8 @@ fn test_mojo_client_redirect_external_req_301(client: MojoClient) raises:
except e:
print(e)

fn test_mojo_client_lightbug_external_req_200(client: MojoClient) raises:
fn test_mojo_client_lightbug_external_req_200() raises:
var client = MojoClient()
var req = HTTPRequest(
uri=URI.parse_raises("http://httpbin.org/status/200"),
headers=Headers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ from testing import assert_equal, assert_true
from lightbug_http.utils import ByteReader
from lightbug_http.header import Headers, Header
from lightbug_http.io.bytes import Bytes, bytes
from lightbug_http.strings import empty_string
from lightbug_http.net import default_buffer_size


def test_header():
test_parse_request_header()
test_parse_response_header()
test_header_case_insensitive()


def test_header_case_insensitive():
var headers = Headers(Header("Host", "SomeHost"))
Expand Down
5 changes: 1 addition & 4 deletions tests/test_http.mojo → tests/lightbug_http/test_http.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ from lightbug_http.http import HTTPRequest, HTTPResponse, encode
from lightbug_http.header import Header, Headers, HeaderKey
from lightbug_http.uri import URI
from lightbug_http.strings import to_string
from tests.utils import default_server_conn_string

alias default_server_conn_string = "http://localhost:8080"

def test_http():
test_encode_http_request()
test_encode_http_response()


def test_encode_http_request():
Expand Down
File renamed without changes.
11 changes: 0 additions & 11 deletions tests/test_uri.mojo → tests/lightbug_http/test_uri.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ from lightbug_http.strings import empty_string, to_string
from lightbug_http.io.bytes import Bytes


def test_uri():
test_uri_no_parse_defaults()
test_uri_parse_http_with_port()
test_uri_parse_https_with_port()
test_uri_parse_http_with_path()
test_uri_parse_https_with_path()
test_uri_parse_http_basic()
test_uri_parse_http_basic_www()
test_uri_parse_http_with_query_string()
test_uri_parse_http_with_hash()


def test_uri_no_parse_defaults():
var uri = URI.parse("http://example.com")[URI]
Expand Down
1 change: 1 addition & 0 deletions testutils/__init__.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .utils import *
2 changes: 1 addition & 1 deletion tests/utils.mojo → testutils/utils.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct FakeClient(Client):
self.req_host = ""
self.req_is_tls = False

fn do(self, req: HTTPRequest) raises -> HTTPResponse:
fn do(self, owned req: HTTPRequest) raises -> HTTPResponse:
return OK(String(defaultExpectedGetResponse))

fn extract(inout self, req: HTTPRequest) raises -> ReqInfo:
Expand Down

0 comments on commit 6214c13

Please sign in to comment.