def test_announcer_under_abnormal_circumstances():
    mock_serverset = create_autospec(spec=ServerSet, instance=True)
    mock_serverset.join = MagicMock()
    mock_serverset.join.side_effect = [
        KazooException('Whoops the ensemble is down!'),
        'member0001',
    ]
    mock_serverset.cancel = MagicMock()

    endpoint = Endpoint('localhost', 12345)
    clock = ThreadedClock(31337.0)

    announcer = Announcer(mock_serverset,
                          endpoint,
                          clock=clock,
                          exception_wait=Amount(2, Time.SECONDS))
    announcer.start()

    try:
        clock.tick(1.0)
        assert announcer.disconnected_time() == 1.0
        clock.tick(2.0)
        assert announcer.disconnected_time() == 0.0, (
            'Announcer should recover after an exception thrown internally.')
        assert announcer._membership == 'member0001'
    finally:
        announcer.stop()
Beispiel #2
0
def test_announcer_under_abnormal_circumstances():
  mock_serverset = create_autospec(spec=ServerSet, instance=True)
  mock_serverset.join = MagicMock()
  mock_serverset.join.side_effect = [
      KazooException('Whoops the ensemble is down!'),
      'member0001',
  ]
  mock_serverset.cancel = MagicMock()

  endpoint = Endpoint('localhost', 12345)
  clock = ThreadedClock(31337.0)

  announcer = Announcer(
       mock_serverset, endpoint, clock=clock, exception_wait=Amount(2, Time.SECONDS))
  announcer.start()

  try:
    clock.tick(1.0)
    assert announcer.disconnected_time() == 1.0
    clock.tick(2.0)
    assert announcer.disconnected_time() == 0.0, (
        'Announcer should recover after an exception thrown internally.')
    assert announcer._membership == 'member0001'
  finally:
    announcer.stop()
def make_job(role, environment, name, primary_port, portmap, zk_path=None):
  from apache.aurora.config.schema.base import (
      Announcer,
      Job,
      Process,
      Resources,
      Task,
  )
  task = Task(
      name='ignore2',
      processes=[Process(name='ignore3', cmdline='ignore4')],
      resources=Resources(cpu=1, ram=1, disk=1))
  if zk_path:
    announce = Announcer(primary_port=primary_port, portmap=portmap, zk_path=zk_path)
  else:
    announce = Announcer(primary_port=primary_port, portmap=portmap)
  job = Job(
      role=role,
      environment=environment,
      name=name,
      cluster='ignore1',
      task=task,
      announce=announce)
  return job
def test_announcer_under_abnormal_circumstances():
    mock_serverset = mock.MagicMock(spec=ServerSet)
    mock_serverset.join = mock.MagicMock()
    mock_serverset.join.side_effect = [KazooException("Whoops the ensemble is down!"), "member0001"]
    mock_serverset.cancel = mock.MagicMock()

    endpoint = Endpoint("localhost", 12345)
    clock = ThreadedClock(31337.0)

    announcer = Announcer(mock_serverset, endpoint, clock=clock, exception_wait=Amount(2, Time.SECONDS))
    announcer.start()

    try:
        clock.tick(1.0)
        assert announcer.disconnected_time() == 1.0
        clock.tick(2.0)
        assert announcer.disconnected_time() == 0.0, "Announcer should recover after an exception thrown internally."
        assert announcer._membership == "member0001"
    finally:
        announcer.stop()
def test_announcer_under_normal_circumstances():
    joined = threading.Event()

    def joined_side_effect(*args, **kw):
        joined.set()
        return 'membership foo'

    mock_serverset = create_autospec(spec=ServerSet, instance=True)
    mock_serverset.join = MagicMock()
    mock_serverset.join.side_effect = joined_side_effect
    mock_serverset.cancel = MagicMock()

    endpoint = Endpoint('localhost', 12345)
    clock = ThreadedClock(31337.0)

    announcer = Announcer(mock_serverset, endpoint, clock=clock)
    assert announcer.disconnected_time() == 0.0
    clock.tick(1.0)
    assert announcer.disconnected_time() == 1.0, (
        'Announcer should advance disconnection time when not yet initially connected.'
    )

    announcer.start()

    try:
        joined.wait(timeout=1.0)
        assert joined.is_set()

        assert announcer.disconnected_time() == 0.0
        clock.tick(1.0)
        assert announcer.disconnected_time() == 0.0, (
            'Announcer should not advance disconnection time when connected.')
        assert announcer._membership == 'membership foo'

    finally:
        announcer.stop()

    mock_serverset.cancel.assert_called_with('membership foo')

    assert announcer.disconnected_time() == 0.0
    clock.tick(1.0)
    assert announcer.disconnected_time() == 0.0, (
        'Announcer should not advance disconnection time when stopped.')
def test_announcer_on_expiration():
    joined = threading.Event()
    operations = []

    def joined_side_effect(*args, **kw):
        # 'global' does not work within python nested functions, so we cannot use a
        # counter here, so instead we do append/len (see PEP-3104)
        operations.append(1)
        if len(operations) == 1 or len(operations) == 3:
            joined.set()
            return 'membership %d' % len(operations)
        else:
            raise KazooException('Failed to reconnect')

    mock_serverset = create_autospec(spec=ServerSet, instance=True)
    mock_serverset.join = MagicMock()
    mock_serverset.join.side_effect = joined_side_effect
    mock_serverset.cancel = MagicMock()

    endpoint = Endpoint('localhost', 12345)
    clock = ThreadedClock(31337.0)

    announcer = Announcer(mock_serverset,
                          endpoint,
                          clock=clock,
                          exception_wait=Amount(2, Time.SECONDS))
    announcer.start()

    try:
        joined.wait(timeout=1.0)
        assert joined.is_set()
        assert announcer._membership == 'membership 1'
        assert announcer.disconnected_time() == 0.0
        clock.tick(1.0)
        assert announcer.disconnected_time() == 0.0
        announcer.on_expiration()  # expect exception
        clock.tick(1.0)
        assert announcer.disconnected_time() == 1.0, (
            'Announcer should be disconnected on expiration.')
        clock.tick(10.0)
        assert announcer.disconnected_time() == 0.0, (
            'Announcer should not advance disconnection time when connected.')
        assert announcer._membership == 'membership 3'

    finally:
        announcer.stop()
Beispiel #7
0
def test_announcer_under_normal_circumstances():
  joined = threading.Event()

  def joined_side_effect(*args, **kw):
    joined.set()
    return 'membership foo'

  mock_serverset = create_autospec(spec=ServerSet, instance=True)
  mock_serverset.join = MagicMock()
  mock_serverset.join.side_effect = joined_side_effect
  mock_serverset.cancel = MagicMock()

  endpoint = Endpoint('localhost', 12345)
  clock = ThreadedClock(31337.0)

  announcer = Announcer(mock_serverset, endpoint, clock=clock)
  assert announcer.disconnected_time() == 0.0
  clock.tick(1.0)
  assert announcer.disconnected_time() == 1.0, (
      'Announcer should advance disconnection time when not yet initially connected.')

  announcer.start()

  try:
    joined.wait(timeout=1.0)
    assert joined.is_set()

    assert announcer.disconnected_time() == 0.0
    clock.tick(1.0)
    assert announcer.disconnected_time() == 0.0, (
        'Announcer should not advance disconnection time when connected.')
    assert announcer._membership == 'membership foo'

  finally:
    announcer.stop()

  mock_serverset.cancel.assert_called_with('membership foo')

  assert announcer.disconnected_time() == 0.0
  clock.tick(1.0)
  assert announcer.disconnected_time() == 0.0, (
      'Announcer should not advance disconnection time when stopped.')
Beispiel #8
0
def test_announcer_on_expiration():
  joined = threading.Event()
  operations = []

  def joined_side_effect(*args, **kw):
    # 'global' does not work within python nested functions, so we cannot use a
    # counter here, so instead we do append/len (see PEP-3104)
    operations.append(1)
    if len(operations) == 1 or len(operations) == 3:
      joined.set()
      return 'membership %d' % len(operations)
    else:
      raise KazooException('Failed to reconnect')

  mock_serverset = create_autospec(spec=ServerSet, instance=True)
  mock_serverset.join = MagicMock()
  mock_serverset.join.side_effect = joined_side_effect
  mock_serverset.cancel = MagicMock()

  endpoint = Endpoint('localhost', 12345)
  clock = ThreadedClock(31337.0)

  announcer = Announcer(
      mock_serverset, endpoint, clock=clock, exception_wait=Amount(2, Time.SECONDS))
  announcer.start()

  try:
    joined.wait(timeout=1.0)
    assert joined.is_set()
    assert announcer._membership == 'membership 1'
    assert announcer.disconnected_time() == 0.0
    clock.tick(1.0)
    assert announcer.disconnected_time() == 0.0
    announcer.on_expiration()  # expect exception
    clock.tick(1.0)
    assert announcer.disconnected_time() == 1.0, (
        'Announcer should be disconnected on expiration.')
    clock.tick(10.0)
    assert announcer.disconnected_time() == 0.0, (
        'Announcer should not advance disconnection time when connected.')
    assert announcer._membership == 'membership 3'

  finally:
    announcer.stop()