def test_sink(self): om = self.om db = self.db url = str(self.url) source = DatasetSource(om, 'stream-test') sink = DatasetSink(om, 'stream-sink') s = stream('test', url=url) s.attach(source) def emit(window): # this runs in a sep thread, so reconnect db db = connectdb(url) db.processed.insert(window.data) om.datasets.put({'foo': 'bar'}, 'stream-test') sleep(1) em = CountWindow('test', emitfn=emit, forwardfn=sink.put) em.run(blocking=False) sleep(1) s.stop() docs = list(db.processed.find()) docs = list(om.datasets.collection('stream-sink').find()) self.assertEqual(len(docs), 1)
def consumer(q, url): # note the stream decorator blocks the consumer and runs the decorated # function asynchronously upon the window criteria is satisfied om = Omega(mongo_url=url) @streaming('test', size=2, url=url, keep=True, queue=q, sink=DatasetSink(om, 'consumer')) def myprocess(window): return {'myprocess': True, 'data': window.data}