Skip to content

Commit 325c248

Browse files
authored
Make it easy to start the configured server using a bake task. (#10)
1 parent de148ec commit 325c248

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

bake/rack/conform.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2022, by Samuel Williams.
5+
6+
def initialize(...)
7+
super
8+
require 'sus'
9+
require 'rack/conform/server'
10+
end
11+
12+
def server
13+
@server = Rack::Conform::Server.current(log: $stdout)
14+
assertions = Sus::Assertions.new(output: Sus::Output.default)
15+
@server.start(assertions)
16+
17+
begin
18+
@server.wait
19+
rescue Interrupt
20+
# Ignore.
21+
end
22+
end

lib/rack/conform/server.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,27 @@
1111
module Rack
1212
module Conform
1313
class Server
14-
def self.current
14+
def self.current(**options)
1515
command = ENV['RACK_CONFORM_SERVER']
1616
endpoint = ENV['RACK_CONFORM_ENDPOINT']
1717

1818
if command and endpoint
19-
return self.new(command, endpoint)
19+
return self.new(command, endpoint, **options)
2020
end
2121
end
2222

23-
def initialize(command, endpoint)
23+
def initialize(command, endpoint, log: nil)
2424
@command = command
2525
@endpoint = endpoint
26+
@log = log
2627

2728
@pid = nil
2829
end
2930

31+
def log
32+
@log ||= ::File.open("server.log", "w+")
33+
end
34+
3035
def print(output)
3136
output.write "server ", :variable, @command.inspect, " ", @endpoint, :reset
3237
end
@@ -40,6 +45,12 @@ def start(assertions)
4045
end
4146
end
4247

48+
def wait
49+
if @pid
50+
Process.wait(@pid)
51+
end
52+
end
53+
4354
def close
4455
if @pid
4556
Process.kill(:INT, @pid)
@@ -50,7 +61,7 @@ def close
5061

5162
def startup(assertions, timeout: 10)
5263
clock = Sus::Clock.start!
53-
log = ::File.open("server.log", "w+")
64+
log = self.log
5465
pid = Process.spawn(@command, out: log, err: log)
5566

5667
Async do
@@ -78,8 +89,6 @@ def startup(assertions, timeout: 10)
7889
rescue => error
7990
Process.kill(:INT, pid) if pid
8091
raise
81-
ensure
82-
log&.close
8392
end
8493
end
8594
end

rack-conform.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Gem::Specification.new do |spec|
2020
spec.required_ruby_version = ">= 2.5"
2121

2222
spec.add_dependency "async-http", "~> 0.50"
23-
spec.add_dependency "rack", ">= 1.0"
2423
spec.add_dependency "benchmark-http", "~> 0.2"
24+
spec.add_dependency "rack", ">= 1.0"
2525
spec.add_dependency "sus", "~> 0.12"
2626
spec.add_dependency "sus-fixtures-async", "~> 0.1.0"
2727
end

readme.md

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

33
Provides rack server conformance testing.
44

5-
[![Development Status](https://github.com/socketry/protocol-http/workflows/Test/badge.svg)](https://github.com/socketry/protocol-http/actions?workflow=Test)
5+
[![Development Status](https://github.com/socketry/rack-conform/workflows/Test/badge.svg)](https://github.com/socketry/rack-conform/actions?workflow=Test)
66

77
## Motivation
88

@@ -14,20 +14,32 @@ Rack has pretty decent support for validating applications do the right thing us
1414

1515
### Servers Tested
1616

17-
- Falcon (Rack 2 & 3)
18-
- Puma (Rack 2)
19-
- Passenger (Rack 2)
20-
- Unicorn (Rack 2)
21-
- Thin (Rack 2)
17+
- Falcon (Rack 2 & 3)
18+
- Puma (Rack 2)
19+
- Passenger (Rack 2)
20+
- Unicorn (Rack 2)
21+
- Thin (Rack 2)
2222

2323
## Usage
2424

2525
This repository includes test suite execution for published versions of major web servers. You can also run it for a specific server:
2626

27-
```bash
27+
``` bash
2828
export BUNDLE_GEMFILE=gems/falcon-v0-rack-v3.rb
2929
bundle install
3030
export RACK_CONFORM_SERVER="falcon --bind http://localhost:9292"
3131
export RACK_CONFORM_ENDPOINT="http://localhost:9292"
3232
bundle exec sus # run tests
3333
```
34+
35+
### Starting A Server
36+
37+
You can also start a server running the conform application for independent testing (e.g. using `curl`).
38+
39+
``` bash
40+
export BUNDLE_GEMFILE=gems/falcon-v0-rack-v3.rb
41+
bundle install
42+
export RACK_CONFORM_SERVER="falcon --bind http://localhost:9292"
43+
export RACK_CONFORM_ENDPOINT="http://localhost:9292"
44+
bundle exec bake rack:conform:server
45+
```

0 commit comments

Comments
 (0)