Skip to content

Commit

Permalink
ffdb-009: add tests for restarting DB
Browse files Browse the repository at this point in the history
  • Loading branch information
godcrampy committed Jan 13, 2024
1 parent da5cf5d commit 9852252
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/test/java/com/sahilbondre/firefly/FireflyDBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,68 @@ void givenNonexistentKey_whenGet_thenExceptionThrown() throws IOException {
// Attempt to get a nonexistent key
assertThrows(IllegalArgumentException.class, () -> fireflyDB.get(key));
}

@Test
void givenStartedInstance_whenSetMultipleTimes_thenValuesAreCorrect() throws IOException {
// Given
fireflyDB.start();
assertTrue(fireflyDB.isStarted());

// Set a value
byte[] key = "testKey".getBytes();
byte[] value = "testValue".getBytes();
fireflyDB.set(key, value);

// Set another value
byte[] key2 = "testKey2".getBytes();
byte[] value2 = "testValue2".getBytes();
fireflyDB.set(key2, value2);

// Get the values
byte[] retrievedValue = fireflyDB.get(key);
byte[] retrievedValue2 = fireflyDB.get(key2);
assertArrayEquals(value, retrievedValue);
assertArrayEquals(value2, retrievedValue2);
}

@Test
void givenStartedInstance_whenSetSameKeyMultipleTimes_thenValueIsCorrect() throws IOException {
// Given
fireflyDB.start();
assertTrue(fireflyDB.isStarted());

// When
// Set a value
byte[] key = "testKey".getBytes();
byte[] value = "testValue".getBytes();
fireflyDB.set(key, value);

// Set another value
byte[] value2 = "testValue2".getBytes();
fireflyDB.set(key, value2);

// Get the values
byte[] retrievedValue = fireflyDB.get(key);
assertArrayEquals(value2, retrievedValue);
}

@Test
void givenStartedInstance_whenSetAndRestart_thenValueIsCorrect() throws IOException {
// Given
fireflyDB.start();
assertTrue(fireflyDB.isStarted());
byte[] key = "testKey".getBytes();
byte[] value = "testValue".getBytes();
fireflyDB.set(key, value);
fireflyDB.stop();

// When
// Restart the instance
fireflyDB = FireflyDB.getInstance(TEST_FOLDER);
fireflyDB.start();

// Get the values
byte[] retrievedValue = fireflyDB.get(key);
assertArrayEquals(value, retrievedValue);
}
}

0 comments on commit 9852252

Please sign in to comment.