Skip to content

Commit

Permalink
vweb: make vweb_test.v more robust and faster, by embedding its serve…
Browse files Browse the repository at this point in the history
…r as a module
  • Loading branch information
spytheman committed Jan 16, 2024
1 parent 34e4353 commit 3a66355
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 81 deletions.
51 changes: 5 additions & 46 deletions vlib/vweb/tests/vweb_test.v
Original file line number Diff line number Diff line change
@@ -1,60 +1,19 @@
import os
import time
import io
import json
import time
import net
import net.http
import io
import vweb.tests.vweb_test_server

const sport = 12380
const localserver = '127.0.0.1:${sport}'
const exit_after_time = 12000 // milliseconds
const exit_after_time = 12 * time.second

const vexe = os.getenv('VEXE')
const vweb_logfile = os.getenv('VWEB_LOGFILE')
const vroot = os.dir(vexe)
const serverexe = os.join_path(os.cache_dir(), 'vweb_test_server.exe')
const tcp_r_timeout = 30 * time.second
const tcp_w_timeout = 30 * time.second

// setup of vweb webserver
fn testsuite_begin() {
os.chdir(vroot) or {}
if os.exists(serverexe) {
os.rm(serverexe) or {}
}
}

fn test_a_simple_vweb_app_can_be_compiled() {
// did_server_compile := os.system('${os.quoted_path(vexe)} -g -o ${os.quoted_path(serverexe)} vlib/vweb/tests/vweb_test_server.v')
// TODO: find out why it does not compile with -usecache and -g
did_server_compile := os.system('${os.quoted_path(vexe)} -o ${os.quoted_path(serverexe)} vlib/vweb/tests/vweb_test_server.v')
assert did_server_compile == 0
assert os.exists(serverexe)
}

fn test_a_simple_vweb_app_runs_in_the_background() {
mut suffix := ''
$if !windows {
suffix = ' > /dev/null &'
}
if vweb_logfile != '' {
suffix = ' 2>> ${os.quoted_path(vweb_logfile)} >> ${os.quoted_path(vweb_logfile)} &'
}
server_exec_cmd := '${os.quoted_path(serverexe)} ${sport} ${exit_after_time} ${suffix}'
$if debug_net_socket_client ? {
eprintln('running:\n${server_exec_cmd}')
}
$if windows {
spawn os.system(server_exec_cmd)
} $else {
res := os.system(server_exec_cmd)
assert res == 0
}
$if macos {
time.sleep(1000 * time.millisecond)
} $else {
time.sleep(100 * time.millisecond)
}
vweb_test_server.start_in_background(sport, exit_after_time)!
}

// web client tests follow
Expand Down
Original file line number Diff line number Diff line change
@@ -1,105 +1,118 @@
module main
module vweb_test_server

import os
import log
import vweb
import time

const known_users = ['bilbo', 'kent']

struct App {
vweb.Context
port int
timeout int
global_config shared Config
}

struct Config {
max_ping int
@[if verbose_vweb_server ?]
fn linfo(s string) {
log.info(s)
}

fn exit_after_timeout(timeout_in_ms int) {
time.sleep(timeout_in_ms * time.millisecond)
println('>> webserver: pid: ${os.getpid()}, exiting ...')
exit(0)
}

fn main() {
if os.args.len != 3 {
panic('Usage: `vweb_test_server.exe PORT TIMEOUT_IN_MILLISECONDS`')
}
http_port := os.args[1].int()
pub fn start_in_background(http_port int, timeout time.Duration) ! {
linfo('>> ${@FN}, http_port: ${http_port}, timeout: ${timeout}')
assert http_port > 0
timeout := os.args[2].int()
assert timeout > 0
spawn exit_after_timeout(timeout)
//
spawn fn (timeout time.Duration) {
time.sleep(timeout)
linfo('>> webserver: pid: ${os.getpid()}, exiting cleanly after ${timeout.milliseconds()}ms ...')
exit(0)
}(timeout)
shared config := &Config{
max_ping: 50
}
app := &App{
port: http_port
timeout: timeout
global_config: config
}
eprintln('>> webserver: pid: ${os.getpid()}, started on http://localhost:${app.port}/ , with maximum runtime of ${app.timeout} milliseconds.')
vweb.run_at(app, host: 'localhost', port: http_port, family: .ip)!
linfo('>> webserver: pid: ${os.getpid()}, started on http://localhost:${app.port}/ , with maximum runtime of ${timeout.milliseconds()} ms.')
spawn fn (app &App, http_port int) {
vweb.run_at(app, host: 'localhost', port: http_port, family: .ip) or {}
}(app, http_port)
_ := <-app.is_ready
log.info('>> ${@FN} finished, http_port: ${http_port}, timeout: ${timeout}')
}

// pub fn (mut app App) init_server() {
//}
const known_users = ['bilbo', 'kent']

struct App {
vweb.Context
port int
global_config shared Config
is_ready chan bool
}

struct Config {
max_ping int
}

pub fn (mut app App) before_accept_loop() {
linfo('>>>>> ${@LOCATION}')
app.is_ready <- true
}

pub fn (mut app App) index() vweb.Result {
linfo('>>>>> ${@LOCATION}')
rlock app.global_config {
assert app.global_config.max_ping == 50
}
return app.text('Welcome to VWeb')
}

pub fn (mut app App) simple() vweb.Result {
linfo('>>>>> ${@LOCATION}')
return app.text('A simple result')
}

pub fn (mut app App) html_page() vweb.Result {
linfo('>>>>> ${@LOCATION}')
return app.html('<h1>ok</h1>')
}

// the following serve custom routes
@['/:user/settings']
pub fn (mut app App) settings(username string) vweb.Result {
if username !in known_users {
linfo('>>>>> ${@LOCATION}, username: ${username}')
if username !in vweb_test_server.known_users {
return app.not_found()
}
return app.html('username: ${username}')
}

@['/:user/:repo/settings']
pub fn (mut app App) user_repo_settings(username string, repository string) vweb.Result {
if username !in known_users {
linfo('>>>>> ${@LOCATION}, username: ${username}, repository: ${repository}')
if username !in vweb_test_server.known_users {
return app.not_found()
}
return app.html('username: ${username} | repository: ${repository}')
}

@['/json_echo'; post]
pub fn (mut app App) json_echo() vweb.Result {
// eprintln('>>>>> received http request at /json_echo is: $app.req')
linfo('>>>>> ${@LOCATION} received http request at /json_echo is: ${app.req}')
app.set_content_type(app.req.header.get(.content_type) or { '' })
return app.ok(app.req.data)
}

@['/login'; post]
pub fn (mut app App) login_form(username string, password string) vweb.Result {
linfo('>>>>> ${@LOCATION}, username: ${username}, password: ${password}')
return app.html('username: x${username}x | password: x${password}x')
}

@['/form_echo'; post]
pub fn (mut app App) form_echo() vweb.Result {
linfo('>>>>> ${@LOCATION}')
app.set_content_type(app.req.header.get(.content_type) or { '' })
return app.ok(app.form['foo'])
}

@['/file_echo'; post]
pub fn (mut app App) file_echo() vweb.Result {
linfo('>>>>> ${@LOCATION}')
if 'file' !in app.files {
app.set_status(500, '')
return app.text('no file')
Expand All @@ -111,24 +124,27 @@ pub fn (mut app App) file_echo() vweb.Result {
// Make sure [post] works without the path
@[post]
pub fn (mut app App) json() vweb.Result {
// eprintln('>>>>> received http request at /json is: $app.req')
linfo('>>>>> ${@LOCATION} received http request is: ${app.req}')
app.set_content_type(app.req.header.get(.content_type) or { '' })
return app.ok(app.req.data)
}

// Custom 404 page
pub fn (mut app App) not_found() vweb.Result {
linfo('>>>>> ${@LOCATION}')
app.set_status(404, 'Not Found')
return app.html('404 on "${app.req.url}"')
}

@[host: 'example.com']
@['/with_host']
pub fn (mut app App) with_host() vweb.Result {
linfo('>>>>> ${@LOCATION}')
return app.ok('')
}

pub fn (mut app App) shutdown() vweb.Result {
linfo('>>>>> ${@LOCATION}')
session_key := app.get_cookie('skey') or { return app.not_found() }
if session_key != 'superman' {
return app.not_found()
Expand All @@ -138,7 +154,7 @@ pub fn (mut app App) shutdown() vweb.Result {
}

fn (mut app App) exit_gracefully() {
eprintln('>> webserver: exit_gracefully')
linfo('>>>>> ${@LOCATION}')
time.sleep(100 * time.millisecond)
exit(0)
}

0 comments on commit 3a66355

Please sign in to comment.