Example #1
0
def random_block_info():
    locations = []
    for _ in range(random.randint(1, 10)):
        locations.append(random_block_location())
    return wire.BlockInfo(block_id=random_int(),
                          length=random_int(),
                          locations=locations)
Example #2
0
def random_file_info():
    block_ids = []
    for _ in range(random.randint(1, 10)):
        block_ids.append(random_int())
    file_block_infos = []
    for _ in range(random.randint(1, 10)):
        file_block_infos.append(random_file_block_info())
    return wire.FileInfo(block_ids=block_ids,
                         block_size_bytes=random_int(),
                         cacheable=random_bool(),
                         completed=random_bool(),
                         creation_time_ms=random_int(),
                         file_block_infos=file_block_infos,
                         file_id=random_int(),
                         folder=random_bool(),
                         group=random_str(),
                         in_memory_percentage=random_int(),
                         last_modification_time_ms=random_int(),
                         length=random_int(),
                         mode=random_int(),
                         mount_point=random_bool(),
                         name=random_str(),
                         owner=random_str(),
                         path=random_str(),
                         persisted=random_bool(),
                         persistence_state=random_persistence_state(),
                         pinned=random_bool(),
                         ttl=random_int(),
                         ttl_action=random_ttl_action(),
                         ufs_path=random_str())
Example #3
0
def random_create_file():
    return option.CreateFile(block_size_bytes=random_int(),
                             location_policy_class=random_str(),
                             mode=random_mode(),
                             recursive=random_bool(),
                             ttl=random_int(),
                             ttl_action=random_ttl_action(),
                             write_type=random_write_type(),
                             replication_durable=random_int(),
                             replication_max=random_int(),
                             replication_min=random_int())
Example #4
0
def test_random():
    s = '\n'
    s += 'random_int(1) = ' + str(util.random_int(1)) + '\n'
    s += '\n'
    s += 'random_int(10) = ' + str(util.random_int(10)) + '\n'
    s += '\n'
    s += 'random_int(5, 20) = ' + str(util.random_int(5, 20)) + '\n'
    s += '\n'
    s += 'random_int(a, b, step) = ' + str(util.random_int(10, 20, 5)) + '\n'

    s += '\n'
    s += 'random_float(5, 10) = ' + str(util.random_float(5, 10)) + '\n'
    s += 'random_float(-1, 1) = ' + str(util.random_float(-1, 1)) + '\n'
    s += 'random_float(0, 1) = ' + str(util.random_float(0, 1)) + '\n'
    s += 'random_float(1.9, 2) = ' + str(util.random_float(1.9, 2)) + '\n'

    s += 'random_float(0, 1.1) = ' + str(util.random_float(0, 1.1)) + '\n'

    s += '\n'
    s += 'random_bool() = ' + str(util.random_bool()) + '\n'
    s += 'random_bool(0) = ' + str(util.random_bool(0)) + '\n'
    s += 'random_bool(1/10) = ' + str(util.random_bool(1 / 10)) + '\n'
    s += 'random_bool(1/90) = ' + str(util.random_bool(90 / 100)) + '\n'
    s += 'random_bool(100/100) = ' + str(util.random_bool(100 / 100)) + '\n'
    s += 'random_bool(2) = ' + str(util.random_bool(2)) + '\n'

    s += '\n'
    s += 'random_ascii() = ' + util.random_ascii() + '\n'

    s += '\n'
    s += 'random_str(4) = "' + util.random_str(4) + '"\n'
    s += '\n'
    s += 'random_str(1, 3) = "' + util.random_str(1, 3) + '"\n'

    s += '\n'
    s += 'random_str(4, tbl=\'ABC123\') = "' + util.random_str(
        4, tbl='ABC123') + '"\n'
    s += '\n'
    s += 'random_str(1, 3, tbl=\'ABC123\') = "' + util.random_str(
        1, 3, tbl='ABC123') + '"\n'

    s += '\n'
    s += 'random_str(4, tbl=\'ABC123あいうえお\') = "' + util.random_str(
        4, tbl='ABC123あいうえお') + '"\n'
    s += '\n'
    s += 'random_str(1, 3, tbl=\'ABC123あいうえお\') = "' + util.random_str(
        1, 3, tbl='ABC123あいうえお') + '"\n'

    return s
Example #5
0
def random_file_block_info():
    ufs_locations = []
    for _ in range(random.randint(1, 10)):
        ufs_locations.append(random_str())
    return wire.FileBlockInfo(block_info=random_block_info(),
                              offset=random_int(),
                              ufs_locations=ufs_locations)
Example #6
0
def random_set_attribute():
    return option.SetAttribute(group=random_str(),
                               mode=random_mode(),
                               owner=random_str(),
                               persisted=random_bool(),
                               pinned=random_bool(),
                               recursive=random_bool(),
                               ttl=random_int(),
                               ttl_action=random_ttl_action())
Example #7
0
def test_write():
    file_id = random_int()
    message = random_str()
    client, cleanup = setup_client(
        streams_handler(file_id, 'write', input=message))
    writer = client.write(file_id)
    length = writer.write(message)
    writer.close()
    assert length == len(message)
Example #8
0
def test_read():
    file_id = random_int()
    message = random_str()
    client, cleanup = setup_client(
        streams_handler(file_id, 'read', output=message))
    reader = client.read(file_id)
    got = reader.read()
    reader.close()
    assert got == message
Example #9
0
def test_open_file():
    path = '/foo'
    expected_file_id = random_int()
    option = random_open_file()
    client, cleanup = setup_client(paths_handler(
        path, 'open-file', input=option, output=expected_file_id))
    file_id = client.open_file(path, option)
    cleanup()
    assert file_id == expected_file_id
Example #10
0
def api_random_num():
    '''
	RESTful API: generate a random number in a JSON file
	'''
    rand_num = util.random_int()
    tmp_name = 'random_number'
    json_dict = {'name': tmp_name, 'number': rand_num}

    # convert dictionary to json obj
    json_obj = json.dumps(json_dict)
    return json_obj
Example #11
0
def test_open_read():
    path = '/foo'
    file_id = random_int()
    message = random_str()
    handler = combined_handler(
        path, 'open-file', file_id, 'read', path_output=file_id, stream_output=message)
    client, cleanup = setup_client(handler)
    got = None
    with client.open(path, 'r') as f:
        got = f.read()
    cleanup()
    assert got == message.encode()
Example #12
0
def test_open_write():
    path = '/foo'
    file_id = random_int()
    message = random_str()
    handler = combined_handler(path, 'create-file', file_id, 'write',
                               path_output=file_id, stream_input=message)
    client, cleanup = setup_client(handler)
    written_len = None
    with client.open(path, 'w') as f:
        written_len = f.write(message)
    cleanup()
    assert written_len == len(message)
Example #13
0
def api_random_num():
	'''
	RESTful API: generate a random number in a JSON file
	'''
	rand_num = util.random_int()
	tmp_name = 'random_number'
	json_dict = {
		'name': tmp_name,
		'number': rand_num
	}

	# convert dictionary to json obj
	json_obj = json.dumps(json_dict)
	return json_obj
Example #14
0
    def play_round(self):
        # setup: randomization
        self.round_num += 1
        self._player_choices = []
        random.shuffle(self.players)
        P = len(self.players)
        m = util.random_int(0, P * (P - 1))

        # phase 1: players make decisions
        hunts = {}
        for player in self.players:
            other_players = [p for p in self.players if p != player]
            reputations = [p.reputation for p in other_players]
            choices = player.hunt_choices(self.round_num, player.food,
                                          player.reputation, m, reputations)
            hunts[player] = dict(zip(other_players, choices))
            self._player_choices.extend(choices)

        # phase 2: process hunt outcomes
        for player in self.players:
            food_earnings = []
            for other_player, decision in hunts[player].iteritems():
                other_decision = hunts[other_player][player]
                food_earnings.append(score(decision, other_decision))
            player.hunt_outcomes(food_earnings)

        # phase 3: reward altruism
        number_hunters = self._player_choices.count(HUNT)
        award = 2 * (P - 1) if number_hunters > m else 0
        for player in self.players:
            player.round_end(award, m, number_hunters)

        # cleanup: eliminate starved players
        live_players = []
        for player in self.players:
            if player.is_dead():
                self._dead_players.append(player)
            else:
                live_players.append(player)
        self.players = live_players
Example #15
0
def random_open_file():
    return option.OpenFile(cache_location_policy_class=random_str(),
                           ufs_read_location_policy_class=random_str(),
                           max_ufs_read_concurrency=random_int(),
                           read_type=random_read_type())
Example #16
0
def random_block_location():
    return wire.BlockLocation(worker_id=random_int(),
                              worker_address=random_worker_net_address(),
                              tier_alias=random_str())
Example #17
0
def random_worker_net_address():
    return wire.WorkerNetAddress(host=random_str(),
                                 rpc_port=random_int(),
                                 data_port=random_int(),
                                 web_port=random_int())
Example #18
0
 def read(self):
     return random_int(self.min, self.max)
Example #19
0
 def random_map_point(self):
     return (util.random_int(0, self.mapx), util.random_int(0, self.mapy))
Example #20
0
def test_close():
    file_id = random_int()
    client, cleanup = setup_client(streams_handler(file_id, 'close'))
    client.close(file_id)
    cleanup()