def test_with_buffering(self): fruits = [ 'apple', 'banana', 'cherry', 'durian', 'guava', 'kiwi', 'lemon', 'orange', 'peach', 'pear', 'quince', 'strawberry' ] stathasher_file = tempfile.NamedTemporaryFile() stathasher_file.write("carbon:\n bind: 127.0.0.1:2004\n " "shard_map: {0: '127.0.0.1:2000', " "1: '127.0.0.1:2000', " "2: '127.0.0.1:2000', " "3: '127.0.0.1:2000'}\n") stathasher_file.flush() self.hasher = subprocess.Popen([ carbonsink.MetricHandler.STATHASHER_PATH, '-c', stathasher_file.name ], stdin=subprocess.PIPE, stdout=subprocess.PIPE) fruit_hash = {} for fruit in fruits: fruit_key = '%s.%s' % ('prefix', fruit) fruit_hash[fruit] = carbonsink.get_hash(self.hasher, fruit_key)['carbon_shard'] # ensure that all shards are accounted for self.assertEqual(set(fruit_hash.values()), set([0, 1, 2, 3])) # pick two random shards to buffer buffer_shards = random.sample([0, 1, 2, 3], 2) buffer_file = StringIO.StringIO('\n'.join(map(str, buffer_shards))) self.tempd = tempfile.mkdtemp() with carbonsink.metric_handler([self.addr], 'prefix') as handler: handler.detect_buffer_shards(buffer_file, self.tempd, stathasher_file.name) for fruit in fruits: carbon_line = 'prefix.%s foo 1' % (fruit, ) shard_num = fruit_hash[fruit] handler.handle_metric('%s|foo|1' % (fruit, )) if shard_num not in buffer_shards: # check that the nc process received the data self.check_line(carbon_line) else: # open the buffer file and get the last line filename = os.path.join(self.tempd, 'shard_%d.txt' % (shard_num, )) with open(filename) as log_file: for line in log_file: pass # ensure the final line is the buffered line self.assertEqual(carbon_line + '\n', line)
def test_with_buffering(self): fruits = ['apple', 'banana', 'cherry', 'durian', 'guava', 'kiwi', 'lemon', 'orange', 'peach', 'pear', 'quince', 'strawberry'] stathasher_file = tempfile.NamedTemporaryFile() stathasher_file.write("carbon:\n bind: 127.0.0.1:2004\n " "shard_map: {0: '127.0.0.1:2000', " "1: '127.0.0.1:2000', " "2: '127.0.0.1:2000', " "3: '127.0.0.1:2000'}\n") stathasher_file.flush() self.hasher = subprocess.Popen( [carbonsink.MetricHandler.STATHASHER_PATH, '-c', stathasher_file.name], stdin=subprocess.PIPE, stdout=subprocess.PIPE) fruit_hash = {} for fruit in fruits: fruit_key = '%s.%s' % ('prefix', fruit) fruit_hash[fruit] = carbonsink.get_hash( self.hasher, fruit_key)['carbon_shard'] # ensure that all shards are accounted for self.assertEqual(set(fruit_hash.values()), set([0, 1, 2, 3])) # pick two random shards to buffer buffer_shards = random.sample([0, 1, 2, 3], 2) buffer_file = StringIO.StringIO('\n'.join(map(str, buffer_shards))) self.tempd = tempfile.mkdtemp() with carbonsink.metric_handler([self.addr], 'prefix') as handler: handler.detect_buffer_shards( buffer_file, self.tempd, stathasher_file.name) for fruit in fruits: carbon_line = 'prefix.%s foo 1' % (fruit,) shard_num = fruit_hash[fruit] handler.handle_metric('%s|foo|1' % (fruit,)) if shard_num not in buffer_shards: # check that the nc process received the data self.check_line(carbon_line) else: # open the buffer file and get the last line filename = os.path.join( self.tempd, 'shard_%d.txt' % (shard_num,)) with open(filename) as log_file: for line in log_file: pass # ensure the final line is the buffered line self.assertEqual(carbon_line + '\n', line)
def test_no_buffering(self): with carbonsink.metric_handler([self.addr], 'prefix') as handler: self.check_monitoring_contents('') handler.add_monitoring_sink(self.tempfile.name, 'mon') self.check_monitoring_contents('') # handle a simple metric handler.handle_metric('foo|bar|1') self.check_monitoring_contents('') self.check_line('prefix.foo bar 1') # test the monitoring metric handler.handle_metric('mon|foo|1') self.check_monitoring_contents('foo\n') self.check_line('prefix.mon foo 1') # test again without the monitoring metric handler.handle_metric('quux|baz|1') self.check_monitoring_contents('foo\n') self.check_line('prefix.quux baz 1')