63
63
class ForkedFakeService:
64
64
"""A Grackle service fake, as a ContextManager."""
66
def __init__(self, port, messages=None, write_logs=False):
66
def __init__(self, port, message_archives=None, write_logs=False):
69
69
:param port: The tcp port to use.
70
:param messages: A dict of lists of dicts representing messages. The
71
outer dict represents the archive, the list represents the list of
72
messages for that archive.
70
:param message_archives: A dict of lists of dicts representing
71
archives of messages. The outer dict represents the archive,
72
the list represents the list of messages for that archive.
73
73
:param write_logs: If true, log messages will be written to stdout.
77
if message_archives is None:
78
self.message_archives = {}
80
self.messages = messages
80
self.message_archives = message_archives
81
81
self.read_end, self.write_end = os.pipe()
82
82
self.write_logs = write_logs
85
def from_client(client, messages=None):
85
def from_client(client, message_archives=None):
86
86
"""Instantiate a ForkedFakeService from the client.
88
88
:param port: The client to provide service for.
89
:param messages: A dict of lists of dicts representing messages. The
90
outer dict represents the archive, the list represents the list of
91
messages for that archive.
89
:param message_archives: A dict of lists of dicts representing
90
archives of messages. The outer dict represents the archive,
91
the list represents the list of messages for that archive.
93
return ForkedFakeService(client.port, messages)
93
return ForkedFakeService(client.port, message_archives)
95
95
def is_ready(self):
96
96
"""Tell the parent process that the server is ready for writes."""
111
111
def start_server(self):
112
112
"""Start the HTTP server."""
113
113
service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
114
service.store = MemoryStore(self.messages)
115
for archive_id, messages in service.store.messages.iteritems():
114
service.store = MemoryStore(self.message_archives)
115
for archive_id, messages in service.store.message_archives.iteritems():
116
116
for message in messages:
117
117
message.setdefault('headers', {})