def test_active_server_does_not_change_if_paths_updated(): PATH = '/path' SERVERS = ['http://server1', 'http://server2', 'http://server3'] paths = {PATH: SERVERS} b = Balancer() b.update(paths) # Notify that active server has failed b.notify_error() assert [SERVERS[1]] == [s.server for s in b.actives] b.update(paths) assert [SERVERS[1]] == [s.server for s in b.actives]
def test_notify_error_should_rotate_servers_while_there_are_available_servers(): PATH1 = '/path1' PATH2 = '/path2' SERVER1 = 'http://server1' SERVER2 = 'http://server2' SERVERS = [SERVER1, SERVER2] paths = {PATH1: SERVERS, PATH2: SERVERS} b = Balancer() b.update(paths) b.notify_error() b.notify_error() assert list(b.actives) == [PlaylistResource(SERVER1, PATH1), PlaylistResource(SERVER1, PATH2)]
def test_if_server_fails_for_any_stream_all_streams_should_switch_server(): PATH1 = '/path1' PATH2 = '/path2' SERVER1 = 'http://server1' SERVER2 = 'http://server2' SERVERS = [SERVER1, SERVER2] paths = {PATH1: SERVERS, PATH2: SERVERS} b = Balancer() b.update(paths) assert list(b.actives) == [PlaylistResource(SERVER1, PATH1), PlaylistResource(SERVER1, PATH2)] b.notify_error() assert list(b.actives) == [PlaylistResource(SERVER2, PATH1), PlaylistResource(SERVER2, PATH2)]
def test_notify_error_should_rotate_servers_while_there_are_available_servers( ): PATH1 = '/path1' PATH2 = '/path2' SERVER1 = 'http://server1' SERVER2 = 'http://server2' SERVERS = [SERVER1, SERVER2] paths = {PATH1: SERVERS, PATH2: SERVERS} b = Balancer() b.update(paths) b.notify_error() b.notify_error() assert list(b.actives) == [ PlaylistResource(SERVER1, PATH1), PlaylistResource(SERVER1, PATH2) ]
def test_if_server_fails_for_any_stream_all_streams_should_switch_server(): PATH1 = '/path1' PATH2 = '/path2' SERVER1 = 'http://server1' SERVER2 = 'http://server2' SERVERS = [SERVER1, SERVER2] paths = {PATH1: SERVERS, PATH2: SERVERS} b = Balancer() b.update(paths) assert list(b.actives) == [ PlaylistResource(SERVER1, PATH1), PlaylistResource(SERVER1, PATH2) ] b.notify_error() assert list(b.actives) == [ PlaylistResource(SERVER2, PATH1), PlaylistResource(SERVER2, PATH2) ]
def test_consume_from_balancer_should_timeout(tmpdir, monkeypatch): server = Server(M3U8_HOST, M3U8_PORT) playlist = 'slow' uri = '/slow.m3u8' playlists = {'streams': {playlist: {'input-path': uri, 'servers': [server]}}} errors = [] b = Balancer() b.update(get_servers(playlists)) b.notify_error = lambda: errors.append("ERROR") monkeypatch.setattr(logging, 'warning', lambda warn: 0) # just to hide hlsclient warning hlsclient.consumer.consume_from_balancer(b, playlists, str(tmpdir)) assert errors == ["ERROR"]
def test_active_server_changes_if_error_detected(): PATH = '/path' SERVERS = ['http://server1', 'http://server2', 'http://server3'] paths = {PATH: SERVERS} b = Balancer() b.update(paths) # Notify that the active server has failed assert [SERVERS[0]] == [s.server for s in b.actives] b.notify_error() # Assert that the backups assume assert [SERVERS[1]] == [s.server for s in b.actives] b.notify_error() assert [SERVERS[2]] == [s.server for s in b.actives] # Assert that the first server resumes if backup fails b.notify_error() assert [SERVERS[0]] == [s.server for s in b.actives]