from functools import partial
import hook
import json
import os
import requests
import sync
from sync import process_and_run_steps
import sys
import threading

def get_pr_diff(test, pull_request):
    if 'diff' in test:
        diff_file = test['diff']
    else:
        diff_file = str(pull_request['number']) + '.diff'
    with open(os.path.join('tests', diff_file)) as f:
        return f.read()

with open('tests.json') as f:
    tests = json.loads(f.read())

config = {
    'servo_org': 'servo',
    'username': 'servo-wpt-sync',
    'upstream_org': 'jdm',
    'port': 5000,
}

for test in tests:
    with open(os.path.join('tests', test['payload'])) as f:
        payload = json.loads(f.read())

    print(test['name'] + ':'),
    executed = []
    def callback(step):
        global executed
        executed += [step.name]
    def error_callback(dir_name):
        print('saved error snapshot: %s' % dir_name)
    process_and_run_steps(config,
                          test['db'],
                          payload,
                          partial(get_pr_diff, test),
                          True,
                          step_callback=callback,
                          error_callback=error_callback)
    if executed == test['expected']:
        print 'passed'
    else:
        print
        print executed
        print 'vs'
        print test['expected']
        assert(executed == test['expected'])

class ServerThread(object):
    def __init__(self, config):
        thread = threading.Thread(target=self.run, args=(config,))
        thread.daemon = True
        thread.start()

    def run(self, config):
        hook.main(config, {})

print('testing server hook with /test')

for test in tests:
    print(test['name'] + ':'),

    server = ServerThread(config)

    with open(os.path.join('tests', test['payload'])) as f:
        payload = f.read()

    r = requests.post('http://localhost:' + str(config['port']) + '/test', data={'payload': payload})
    if r.status_code != 204:
        print(r.status_code)
    assert(r.status_code == 204)
    r = requests.post('http://localhost:' + str(config['port']) + '/shutdown')
    if r.status_code != 204:
        print(r.status_code)
    assert(r.status_code == 204)

    print('passed')

@@ -1,12 +1,24 @@
+import copy%0A
 from functoo
@@ -1704,38 +1704,54 @@
 /test')%0A%0Afor
+ (i,
  test
+)
  in 
+enumerate(
 tests
+)
 :%0A    print(
@@ -1772,16 +1772,85 @@
  ':'),%0A%0A
+    this_config = copy.deepcopy(config)%0A    this_config%5B'port'%5D += i%0A
     serv
@@ -1867,16 +1867,21 @@
 rThread(
+this_
 config)%0A
@@ -2009,32 +2009,37 @@
 calhost:' + str(
+this_
 config%5B'port'%5D) 
@@ -2215,16 +2215,21 @@
 ' + str(
+this_
 config%5B'

Make server tests use different ports.