Beispiel #1
0
def test_redis_communication_reader_set_batch(
        mock_read_communication_from_buffer, right_to_left, inferred):
    redis = Mock(
        srandmember=Mock(side_effect=[
            [sentinel.buf0, sentinel.buf1],
        ]),
        type=Mock(return_value=b'set'),
    )
    mock_read_communication_from_buffer.side_effect = [
        sentinel.comm0, sentinel.comm1
    ]

    reader = RedisCommunicationReader(redis,
                                      sentinel.key,
                                      key_type=None if inferred else 'set',
                                      add_references=sentinel.add_references,
                                      right_to_left=right_to_left)

    assert reader.batch(
        sentinel.batch_size) == [sentinel.comm0, sentinel.comm1]

    if inferred:
        redis.type.assert_called_once_with(sentinel.key)
    else:
        assert not redis.type.called

    redis.srandmember.assert_called_once_with(sentinel.key,
                                              sentinel.batch_size)

    mock_read_communication_from_buffer.assert_has_calls([
        call(sentinel.buf0, add_references=sentinel.add_references),
        call(sentinel.buf1, add_references=sentinel.add_references),
    ])
def test_redis_communication_reader_set_batch(
        mock_read_communication_from_buffer, right_to_left, inferred):
    redis = Mock(
        srandmember=Mock(side_effect=[
            [sentinel.buf0, sentinel.buf1],
        ]),
        type=Mock(return_value=b'set'),
    )
    mock_read_communication_from_buffer.side_effect = [
        sentinel.comm0, sentinel.comm1
    ]

    reader = RedisCommunicationReader(
        redis, sentinel.key, key_type=None if inferred else 'set',
        add_references=sentinel.add_references,
        right_to_left=right_to_left)

    assert reader.batch(sentinel.batch_size) == [sentinel.comm0, sentinel.comm1]

    if inferred:
        redis.type.assert_called_once_with(sentinel.key)
    else:
        assert not redis.type.called

    redis.srandmember.assert_called_once_with(sentinel.key, sentinel.batch_size)

    mock_read_communication_from_buffer.assert_has_calls([
        call(sentinel.buf0, add_references=sentinel.add_references),
        call(sentinel.buf1, add_references=sentinel.add_references),
    ])
Beispiel #3
0
 def test_set(self):
     key = 'dataset'
     with RedisServer(loglevel='warning') as server:
         redis_db = Redis(port=server.port)
         redis_db.sadd(key, write_communication_to_buffer(self.comm1))
         redis_db.sadd(key, write_communication_to_buffer(self.comm2))
         redis_db.sadd(key, write_communication_to_buffer(self.comm3))
         reader = RedisCommunicationReader(redis_db, key, key_type='set')
         comms = [c for c in reader]
         ids = [c.id for c in comms]
         # assert no duplicates
         self.assertEquals(3, len(ids))
         self.assertEquals(set(['comm-1', 'comm-2', 'comm-3']), set(ids))
         self.assertEquals(3, len(reader))
         self.assertTrue(hasattr(comms[0], 'sentenceForUUID'))
         self.assertTrue(hasattr(comms[1], 'sentenceForUUID'))
         self.assertTrue(hasattr(comms[2], 'sentenceForUUID'))
         batch_ids = [c.id for c in reader.batch(2)]
         # do this weird thing because set(['foo']) != set([u'foo'])
         self.assertTrue(
             ('comm-1' in batch_ids and 'comm-2' in batch_ids) or
             ('comm-1' in batch_ids and 'comm-3' in batch_ids) or
             ('comm-2' in batch_ids and 'comm-3' in batch_ids)
         )
         # assert data still there
         ids = [c.id for c in reader]
         self.assertEquals(set(['comm-1', 'comm-2', 'comm-3']), set(ids))
         self.assertEquals(3, redis_db.scard(key))
Beispiel #4
0
def test_redis_communication_reader_failed_batch(right_to_left, inferred,
                                                 key_type, pop, block):
    redis = Mock(type=Mock(return_value=key_type.encode('utf-8')), )

    reader = RedisCommunicationReader(redis,
                                      sentinel.key,
                                      key_type=None if inferred else key_type,
                                      add_references=sentinel.add_references,
                                      pop=pop,
                                      block=block)

    if inferred:
        redis.type.assert_called_once_with(sentinel.key)
    else:
        assert not redis.type.called

    with raises(Exception):
        reader.batch(sentinel.batch_size)
def test_redis_communication_reader_failed_batch(right_to_left, inferred,
                                                 key_type, pop, block):
    redis = Mock(
        type=Mock(return_value=key_type.encode('utf-8')),
    )

    reader = RedisCommunicationReader(
            redis, sentinel.key, key_type=None if inferred else key_type,
            add_references=sentinel.add_references,
            pop=pop, block=block)

    if inferred:
        redis.type.assert_called_once_with(sentinel.key)
    else:
        assert not redis.type.called

    with raises(Exception):
        reader.batch(sentinel.batch_size)