def test_checkpoint_saving(self): class MyWriter(Writer): def write(self, msg): return True q = get_queue() # populates the queue with some messages q.put(Message(content='foo', checkpoint='foo')) q.put(Message(content='bar', checkpoint='bar')) conf = {'checkpoint_enabled' : True, 'checkpoint_path' : self.checkpoint_path, 'checkpoint_period' : 1} mywriter = MyWriter(q, conf=conf) # after a start, the messages should be consumed mywriter.start() # waits for checkpoint_period time.sleep(2) self.assertEqual(2, mywriter.processed) self.assertEqual('bar', mywriter.last_checkpoint) f = open(self.checkpoint_path, 'r+') self.assertEqual('bar', pickle.load(f)) f.close()
def test_processed_messages_counting(self): class MyWriter(Writer): def write(self, msg): return True q = get_queue() q.put(Message(content=1)) q.put(Message(content=2)) mywriter = MyWriter(q) mywriter.start() # waits for processing time.sleep(0.1) self.assertEqual(2, mywriter.processed)
def read(self): while True: try: print '\n[READER] message: ' self.store(Message(content=sys.stdin.readline())) except Exception, e: self.log.error('error reading') self.log.error(e)
def read(self): while True: try: msg = "%s,%s" % (time.time(), self.arduino.readline()) self.store(Message(content=msg)) except Exception, e: self.log.error('error reading') self.log.error(e)
def test_periodic_scheduling_removing_from_queue(self): class MyWriter(Writer): def write(self, msg): return msg q = get_queue() q.put(Message(content=1)) q.put(Message(content=2)) conf = {'period' : 1} mywriter = MyWriter(q, conf=conf) mywriter.start() #waits period to write messages time.sleep(2) self.assertEqual(0, q.qsize())
def store_results(self): messages = [ Message(checkpoint={'pos': result[0]}, content=dict(zip(self.columns, result[1]))) for result in self.results ] for message in messages: self.store(message) self.set_current_checkpoint(message.checkpoint)
def read(self): try: image = self.cam.getImage() self.store(Message(content=image)) return True except Exception, e: self.log.error('error in storing image') self.log.error(e) return False
def test_store_discarded_messages_due_to_some_fail(self): class MyWriter(Writer): def write(self, msg): if msg == 'foo2' or msg == 'foo3': return False return True q = get_queue(3) q.put(Message(content='foo1')) q.put(Message(content='foo2')) q.put(Message(content='foo3')) mywriter = MyWriter(q, conf={'blockable': False}) mywriter.start() # waits for processing time.sleep(0.1) self.assertEqual(2, mywriter.discarded)
def read(self): try: date = time.strftime("%b %d %H:%M:%S") msg = "<14>%s %s pycollector: %s" % (date, socket.gethostname(), os.popen('acpi').read()) self.store(Message(content=msg)) return True except Exception, e: self.log.error('error reading') self.log.error(e) return False
def read(self): try: target_host = str(self.host) # print "mtr -c %d --split %s" % (self.samples, self.host) p = Popen("mtr -c %d --split %s" % (self.samples, self.host), shell=True, stdout=PIPE, stderr=PIPE, close_fds=True) stdout, stderr = p.communicate('through stdin to stdout') if p.returncode > 0: if p.returncode == 127: # File not found, lets print path self.log.error( "Unable to find mtr, check that it is installed") else: self.log.error("Unable to run mtr: %s" % (stderr.strip())) return False mtr_output = {} maxHop = 0 for line in stdout.split("\n"): if not line: continue line.strip() (hop, address, loss, received, sent, best, avg, worst) = line.split(" ") dataId = "%s-%s" % (hop, address) maxHop = max(int(hop), maxHop) mtr_output[dataId] = dict(hop_address=address, hop=hop, samples=self.samples, loss_packed=loss, sent_packets=sent, avg_rtt=avg, best_rtt=best, worst_rtt=worst, host=target_host) for k in mtr_output: data = k m = Message(content=mtr_output[k]) self.store(m) #date = time.strftime("%b %d %H:%M:%S") return True except Exception, e: self.log.error('error reading') self.log.error(e) return False
def test_periodic_scheduling_calling_write_method(self): q = get_queue() q.put(Message(content=1)) conf = {'period' : 1} mywriter = Writer(q, conf=conf) mywriter.write = MagicMock(return_value=True) mywriter.start() # waits writer period time.sleep(1) mywriter.write.assert_called_with(1)
def process_message(sess, mess): cmd = Popen(mess.getBody(), stdout=PIPE, shell=True) text = cmd.stdout.read() self.store(Message(content=text))