Ejemplo n.º 1
0
 def create_network(num_servers, num_sensors):
     """
     num_servers darab szerverbol allo halozat letrehozasa, melyekhez kapcsolodo
     szenzorok szamat a num_sensors lista adja meg
     :param num_servers: szerverek szama
     :param num_sensors: szenzorok szama szerverenkent (lista vagy egy szam)
     :return: szerverek listaja, szenorok listainak listaja szerverenkent
     """
     if type(num_sensors) is int:
         num_sensors = [num_sensors] * num_servers
     # create servers
     servers = [StorageServer(alive=True, id=1001+i) for i in range(num_servers)]
     # create sensors
     sensors = []
     for i in range(num_servers):
         ss = []
         for j in range(num_sensors[i]):
             sensor = Sensor(9001+100*i+j, [servers[i]])
             servers[i].add_sensor(sensor)
             ss.append(sensor)
         sensors.append(ss)
     # szerverek kozotti teljes halozat letrehozasa
     for i in range(num_servers):
         for j in range(num_servers):
             servers[i].add_neighbour_server(servers[(i+j+1) % num_servers])
     return servers, sensors
Ejemplo n.º 2
0
def test_serverDown():
    h="""
    Szerver leallas ellenorzese.
    Egy szenzornak megadunk ket szervert.
    Ha a main szerver leall, akkor a szenzor a backup szervernek kuldi az adatot.
    """

    print(h)

    mainServer = StorageServer(alive=True, id=1)
    backupServer = StorageServer(alive=True, id=2)

    s_id = 10
    sensor = Sensor(id=s_id, servers=[mainServer, backupServer])

    #adat kuldese a fo szervernek
    data_1 = Data("data_1 to mainServer")
    sensor.send_data(data=data_1)
    assert(data_1 in mainServer.data[s_id])
    assert(backupServer.data == {})

    #mainServer leallitasa
    data_2 = Data("data_2 to backupServer")
    mainServer.alive = False
    sensor.send_data(data=data_2)
    assert(data_2 not in mainServer.data[s_id])
    assert(data_2 in backupServer.dead_servers_data[1][s_id])

    #mainServer feleled
    data_3 = Data("data_3 to mainServer")
    mainServer.alive = True
    sensor.send_data(data=data_3)
    assert(data_3 in mainServer.data[s_id])
    assert(data_3 not in backupServer.dead_servers_data[1][s_id])

    #ha egyik szerver sem kapja meg akkor kivetelt dob
    mainServer.alive = False
    backupServer.alive = False
    data_4 = Data("data_cannot_be_sent")
    ok = False
    try:
        sensor.send_data(data_4)
    except:
        ok = True
    assert(ok)

    print("Test ok")
def test_MultipleServersDown():
    h = """
    A fo es az egyik backup szerver is inaktivva valik.
    A fo szerver feleledese utan igy is meg kell kapnia a kiesett adatokat.
    """

    print(h)

    mainServer = StorageServer(alive=True, id=1001)
    backupServer1 = StorageServer(alive=True, id=1002)
    backupServer2 = StorageServer(alive=True, id=1003)

    mainServer.add_neighbour_server(backupServer1)
    backupServer1.add_neighbour_server(mainServer)

    mainServer.add_neighbour_server(backupServer2)
    backupServer2.add_neighbour_server(mainServer)

    backupServer1.add_neighbour_server(backupServer2)
    backupServer2.add_neighbour_server(backupServer1)

    s_id = 9001
    sensor = Sensor(id=s_id, servers=[])
    mainServer.add_sensor(sensor)

    data_1 = Data("data_1")
    sensor.send_data(data=data_1)
    assert (data_1 in mainServer.data[s_id])
    assert (backupServer1.data == {})

    #mainServer leallitasa
    mainServer.change_alive_state(False)
    data_2 = Data("data_2 during main down")
    sensor.send_data(data=data_2)
    print('BS1 dead:', backupServer1.dead_servers_data)
    print('BS2 other:', backupServer2.other_data)
    assert (data_2 not in mainServer.data[s_id])
    assert (data_2 in backupServer1.dead_servers_data[1001][s_id])
    assert (data_2 in backupServer2.other_data[1001][s_id])

    # backup1 szerver leall
    backupServer1.change_alive_state(False)
    data_3 = Data('data_3 two servers down')
    sensor.send_data(data_3)

    assert (data_3 not in mainServer.data[s_id])
    assert (data_3 not in backupServer1.dead_servers_data[1001][s_id])
    assert (data_3 in backupServer2.dead_servers_data[1001][s_id])

    #main  es backup feleled
    mainServer.change_alive_state(True)
    backupServer1.change_alive_state(True)
    assert (data_2 in mainServer.data[s_id])
    assert (data_3 in mainServer.data[s_id])
Ejemplo n.º 4
0
def test_replicas():
    h = """
    Masolatok keszitesenek ellenorzese
    Letrehozunk 4 db szervert, az egyikhez hozzaadunk 1 szenzort.
    A szenzor altal alkuldott adatnak a sajat szervere mellett ket masik szerveren kell megjelennie
    """

    print(h)

    server1 = StorageServer(alive=True, id=1001)
    server2 = StorageServer(alive=True, id=1002)
    server3 = StorageServer(alive=True, id=1003)
    server4 = StorageServer(alive=True, id=1004)
    sensor1 = Sensor(9001, servers=[])

    # most nincs full mesh, csak server1 van mindenkivel osszekotve
    server1.add_neighbour_server(server2)
    server2.add_neighbour_server(server1)
    server1.add_neighbour_server(server3)
    server3.add_neighbour_server(server1)
    server1.add_neighbour_server(server4)
    server4.add_neighbour_server(server1)

    server1.add_sensor(sensor1)
    #sensor1.redefine_servers([server1, server2, server3, server4])

    assert (len(sensor1.servers) == 4 and sensor1.servers[0] == server1)

    data = Data('simple_test_data')
    sensor1.send_data(data)

    print('Server1 data:', server1.data)
    assert (len(server1.data) == 1)

    print('Server2 replicas:', server2.other_data)
    assert (server2.other_data[1001][9001][0].data[1] == 'simple_test_data')

    print('Server3 replicas:', server3.other_data)
    assert (server3.other_data[1001][9001][0].data[1] == 'simple_test_data')

    print('Server4 replicas:', server4.other_data)
    assert (server4.other_data == {} or NUM_REPLICAS != 2)

    print('Test ok')
def test_dataReceivedAfterRecovery():
    h="""
    Egy szerver ujraeledese utan ra kell tolteni a kihagyott adatokat
    Egy szenzornak megadunk ket szervert.
    A main szerver leallasa utan a szenzor a backup szervernek kuldi az adatokat.
    A main szerver feleledese utan meg kell kapnia a kimaradt adatokat.
    """

    print(h)

    mainServer = StorageServer(alive=True, id=1)
    backupServer = StorageServer(alive=True, id=2)
    mainServer.add_neighbour_server(backupServer)
    backupServer.add_neighbour_server(mainServer)

    s_id = 10
    sensor = Sensor(id=s_id, servers=[mainServer, backupServer])

    data_1 = Data("data_1 to mainServer")
    sensor.send_data(data=data_1)
    assert(data_1 in mainServer.data[s_id])
    assert(backupServer.data == {})

    #mainServer leallitasa
    mainServer.change_alive_state(False)
    data_2 = Data("data_2 during main down")
    sensor.send_data(data=data_2)
    assert(data_2 not in mainServer.data[s_id])
    assert(data_2 in backupServer.dead_servers_data[1][s_id])

    #main feleled
    mainServer.change_alive_state(True)
    assert(data_2 in mainServer.data[s_id])
Ejemplo n.º 6
0
def test_loadTest():
    h="""
    Egy szerverhez csatlakozik 20 szenzor.
    A szenzorok folyamatosan kuldik az adatokat.
    Minden adatnak meg kell erkezni.
    """

    print(h)

    server = StorageServer(alive=True, id=1)
    sensor_number = 20
    sensors = [Sensor(id=100+i, servers=[server]) for i in range(sensor_number)]

    data_num = 100
    for n in range(data_num):
        for sensor in sensors:
            sensor.send_data(Data("data_from_sensor" + str(sensor.id) + "_data" + str(n)))

    for m in range(data_num):
        for sensor in sensors:
            # itt csak a datak ertekevel hasonlitjuk ossze, az idoket nem nezzuk
            assert("data_from_sensor" + str(sensor.id) + "_data" + str(m) in [d.data[1] for d in server.data[sensor.id]])

    print("Test ok")
Ejemplo n.º 7
0
def test_dataSending():
    h="""
    Adatkuldes ellenorzese.
    Letrehozunk egy szervert es hozzaadunk egy szenzort.
    A szenzor elkuld egy adatot; ellenorizzuk, hogy a szerverre megerkezett-e.
    """

    print(h)

    server1 = StorageServer(alive=True, id=1001)
    sensor1 = Sensor(9001, servers=[])

    server1.add_sensor(sensor1)
    sensor1.redefine_servers([server1])

    data = Data('simple_test_data')
    #server1.receive_data('testdata', sensor1.id)
    sensor1.send_data(data)
    print('Server1 data:', server1.data)
    assert(len(server1.data) == 1)

    print('Test ok')
def test_MultipleServersDown():
    h = """
    A fo szerver mellett, mar mukodes kozben megjelenik egy masik szerver.
    A fo szerver ezutan meghal, ekkor a masiknak kell atvennie a helyet.
    A fo szerver feleledese utan meg kell kapnia az adatokat az uj szervertol.
    Ezutan az uj szerver all le, ekkor a fonek kell begyujtenie a keletkezett adatokat.
    """

    print(h)

    mainServer = StorageServer(alive=True, id=1001)

    s_id = 9001
    s2_id = 9002
    sensor = Sensor(id=s_id, servers=[])
    mainServer.add_sensor(sensor)

    data_1 = Data("data_1")
    sensor.send_data(data=data_1)
    assert (data_1 in mainServer.data[s_id])

    newServer = StorageServer(alive=True, id=1002)

    newServer.add_neighbour_server(mainServer)
    mainServer.add_neighbour_server(newServer)

    # mainServer leallitasa
    mainServer.change_alive_state(False)
    data_2 = Data("data_2 during main down")
    sensor.send_data(data=data_2)
    assert (data_2 not in mainServer.data[s_id])
    assert (data_2 in newServer.dead_servers_data[1001][s_id])

    # main feleled
    mainServer.change_alive_state(True)
    assert (data_2 in mainServer.data[s_id])

    # uj szerver is kap szenzort
    sensor2 = Sensor(s2_id, servers=[])
    newServer.add_sensor(sensor2)
    data_3 = Data('data for new server')
    sensor2.send_data(data_3)
    assert (data_3 in newServer.data[s2_id])
    assert (data_3 in mainServer.other_data[1002][s2_id])

    # uj szerver is leall
    newServer.change_alive_state(False)
    data_4 = Data('data during new sever dead')
    sensor2.send_data(data_4)
    assert (data_4 not in newServer.data[s2_id])
    assert (data_4 in mainServer.dead_servers_data[1002][s2_id])

    # uj szerver feleled
    newServer.change_alive_state(True)
    assert (data_4 in newServer.data[s2_id])
Ejemplo n.º 9
0
def test_log():
    h = """
    Szerverek es szenzorok logolasanak tesztelese
    """
    print(h)

    if not LOGGING_ENABLED:
        print(
            "Logolas ki van kapcsolva a config file-ban, a teszt futtatasahoz be kell kapcsolni"
        )
    assert (LOGGING_ENABLED)

    # stdout atallitasa
    captureOutput = StringIO()
    sys.stdout = captureOutput
    expected = []

    # logolas:
    mainServerID = 1000
    mainServer = StorageServer(alive=True, id=mainServerID)
    sensorID = mainServerID + 1
    sensor = Sensor(id=sensorID, servers=[])
    mainServer.add_sensor(sensor=sensor)
    expected.append("Server " + str(mainServerID) + ": Sensor added: " +
                    str(sensor))

    neighbourServerID = 2000
    neighbourServer = StorageServer(alive=True, id=neighbourServerID)
    mainServer.add_neighbour_server(neighbourServer)
    expected.append("Sensor " + str(sensorID) + ": New server list: " +
                    str(sensor.servers))
    expected.append("Server " + str(mainServerID) +
                    ": Neighbour server added: " + str(neighbourServer))

    data_ = "Data sent from sensor to own server"
    msg = Message(sensor_id=sensorID,
                  server_id=mainServerID,
                  content=data_,
                  is_replica=None)
    sensor.send_data(data=data_)
    expected.append("Sensor " + str(sensorID) + ": Sending data " + data_ +
                    " to " + str(mainServer))
    expected.append("Server " + str(mainServerID) + ": Received own data " +
                    str(msg))
    msg.is_replica = True
    expected.append("Server " + str(neighbourServerID) +
                    ": Received other data " + str(msg))

    mainServer.change_alive_state(alive=False)
    expected.append("Server " + str(mainServerID) +
                    ": Alive state changed to False")
    data_dead = "sending data to dead server"
    sensor.send_data(data_dead)
    msg_dead = Message(sensor_id=sensorID,
                       server_id=mainServerID,
                       content=data_dead,
                       is_replica=None)
    expected.append("Server " + str(neighbourServerID) +
                    ": Received dead data " + str(msg_dead))

    mainServer.change_alive_state(False)
    neighbourServer.change_alive_state(False)
    data_notsent = "Data is unable to send"
    try:
        sensor.send_data(data_notsent)
    except:
        print("exception")
    expected.append("Sensor " + str(sensorID) + ": Sending was unsuccessful.")
    expected.append("Sensor " + str(sensorID) +
                    ": Could not send data to any server.")

    # stdout visszaallitas
    sys.stdout = sys.__stdout__

    for expect in expected:
        if (expect not in captureOutput.getvalue()):
            print(expect)
        assert (expect in captureOutput.getvalue())