Example #1
0
    def test_file_size_limit(self):
        """
        Keeps inserting new keys into the database until the database reaches its limit
        When no more keys are allowed to be inserted anymore, it checks the size of the database
        It deletes all the keys before returning so other tests can run smoothly
        """
        # Reduce to maximum limit so the test can execute efficiently
        KeyValueStore._DB_SIZE_LIMIT = 30 * 1024 * 1024  # 30 Mega Bytes
        store = KeyValueStore.open('test')
        keys = []  # To keep track of the keys to delete later

        while True:
            key, value = TestKVS.get_new_key_value(store)
            try:
                store.create(key, value)
                keys.append(key)
            except Exception as e:
                size = TestKVS.get_test_db_size()
                db_limit = KeyValueStore._DB_SIZE_LIMIT
                expression = .95 * db_limit <= size <= 1.05 * db_limit
                self.assertTrue(expression)

                # Delete the keys so other tests can run
                deleted = 0
                for key in keys:
                    store.delete(key)
                    deleted += 1
                print("deleted " + str(deleted) + " keys")
                return
Example #2
0
    def test_create_read(self):
        """
        Creates a key and retrieves and checks that both the values are equal
        """
        store = KeyValueStore.open('test')
        key, value = TestKVS.get_new_key_value(store)

        store.create(key, value)
        returned_value = store.read(key)
        self.assertEqual(value, returned_value)
Example #3
0
    def test_ttl_delete(self):
        """
        Creates a key with ttl = 1
        Waits for two seconds
        And checks that the key is already gone by deleting it
        """
        store = KeyValueStore.open('test')
        key, value = TestKVS.get_new_key_value(store)

        store.create(key, value, 1)
        time.sleep(2)
        self.assertRaises(Exception, store.delete, key)
Example #4
0
    def test_delete(self):
        """
        Creates a key
        Deletes it
        Checks that the store raises exception on reading the key
        """
        store = KeyValueStore.open('test')
        key, value = TestKVS.get_new_key_value(store)

        store.create(key, value)
        store.delete(key)
        self.assertRaises(Exception, store.read, key)
Example #5
0
    def test_another_process(self):
        """
        Opens a data store and makes the other process open the same data store
        Checks if we get the exception
        """
        print('Testing another process access on same file')
        store = KeyValueStore.open('test')

        output = subprocess.check_output(
            "python another_process_for_testing.py",
            shell=True).decode('utf-8')
        print('Other process said: ' + output)

        # Assert that the exception code is as expected
        self.assertEqual(100, int(json.loads(output)['Exception code']))
Example #6
0
    def test_multi_threading(self):
        """
        Creates two threads
        Both the threads accesses the same data store
        One thread creates a key, the other thread tries to read the key and asserts the value
        """
        store = KeyValueStore.open('test')
        key, value = TestKVS.get_new_key_value(store)

        create_thread = threading.Thread(target=TestKVS.create_key_and_wait,
                                         args=(
                                             key,
                                             value,
                                         ))
        read_thread = threading.Thread(target=self.wait_and_read_key,
                                       args=(
                                           key,
                                           value,
                                       ))

        create_thread.start()
        read_thread.start()
Example #7
0
 def create_key_and_wait(cls, key, value):
     """Creates a key and waits for 1 second"""
     store = KeyValueStore.open('test')
     store.create(key, value)
     time.sleep(1)
Example #8
0
 def wait_and_read_key(self, key, value):
     """Waits for half a second and reads the given key and asserts the value"""
     store = KeyValueStore.open('test')
     time.sleep(0.5)
     self.assertEqual(value, store.read(key))
Example #9
0
from kvs import KeyValueStore

store = KeyValueStore.open('haris')

while True:
    print("c-> Create")
    print("r-> Read")
    print("d-> Delete")

    choice = input()

    if choice == 'c':
        print(
            "Enter key, value(only a string and not JSON object), and ttl (space separated)"
        )
        key, value, ttl = input().split()
        value = {'value': value}
        try:
            store.create(key, value, ttl)
        except Exception as e:
            print(e)

    elif choice == 'r':
        print("Enter key")
        key = input()
        try:
            value = store.read(key)
            print(value)

        except Exception as e:
            print(e)
Example #10
0
from kvs import KeyValueStore
import json
"""
Only meant for testing
Opens the data store named test that would be generated while tests are being executed
If test is successful, opening test from here should raise an exception
"""
try:
    store = KeyValueStore.open('test')
except Exception as e:
    json_object = {"Exception message": e.args[0], "Exception code": e.args[1]}
    print(json.dumps(json_object))