@Test public void testSetLatestConstraintTx() throws Exception { // given new GraphDatabaseFactory().newEmbeddedDatabase(testDir.absolutePath()).shutdown(); StoreFactory sf = new StoreFactory( new Config(new HashMap<String, String>(), GraphDatabaseSettings.class), new DefaultIdGeneratorFactory(), new DefaultWindowPoolFactory(), new DefaultFileSystemAbstraction(), StringLogger.DEV_NULL, null); // when NeoStore neoStore = sf.newNeoStore(new File(testDir.absolutePath(), NeoStore.DEFAULT_NAME)); // then the default is 0 assertEquals(0l, neoStore.getLatestConstraintIntroducingTx()); // when neoStore.setLatestConstraintIntroducingTx(10l); // then assertEquals(10l, neoStore.getLatestConstraintIntroducingTx()); // when neoStore.flushAll(); neoStore.close(); neoStore = sf.newNeoStore(new File(testDir.absolutePath(), NeoStore.DEFAULT_NAME)); // then the value should have been stored assertEquals(10l, neoStore.getLatestConstraintIntroducingTx()); neoStore.close(); }
@Test public void shouldBeAbleToRestartServer() throws Exception { // Given String dbDirectory1 = baseDir.directory("db1").getAbsolutePath(); String dbDirectory2 = baseDir.directory("db2").getAbsolutePath(); ConfigurationBuilder config = new PropertyFileConfigurator( AdvancedServerBuilder.server().usingDatabaseDir(dbDirectory1).createPropertiesFiles(), ConsoleLogger.DEV_NULL); // When NeoServer server = cleanup.add(new AdvancedNeoServer(config, graphDbDependencies())); server.start(); assertNotNull(server.getDatabase().getGraph()); assertEquals(dbDirectory1, server.getDatabase().getLocation()); // Change the database location setProperty(config.configuration(), Configurator.DATABASE_LOCATION_PROPERTY_KEY, dbDirectory2); ServerManagement bean = new ServerManagement(server); bean.restartServer(); // Then assertNotNull(server.getDatabase().getGraph()); assertEquals(dbDirectory2, server.getDatabase().getLocation()); }
private ClusterManager.ManagedCluster startClusterSeededWith(File seedDir) { ClusterManager.ManagedCluster cluster = life.add(new ClusterManager.Builder(targetDir.directory()).withSeedDir(seedDir).build()) .getDefaultCluster(); cluster.await(allSeesAllAsAvailable()); return cluster; }
@Test public void shouldAcceptFolderWithCypFilesAsFixtures() throws Exception { // Given two files in the root folder File targetFolder = testDir.directory(); FileUtils.writeToFile( new File(targetFolder, "fixture1.cyp"), "CREATE (u:User)\n" + "CREATE (a:OtherUser)", false); FileUtils.writeToFile( new File(targetFolder, "fixture2.cyp"), "CREATE (u:User)\n" + "CREATE (a:OtherUser)", false); // And given one file in a sub directory File subDir = new File(targetFolder, "subdirectory"); subDir.mkdir(); FileUtils.writeToFile( new File(subDir, "subDirFixture.cyp"), "CREATE (u:User)\n" + "CREATE (a:OtherUser)", false); // When try (ServerControls server = newInProcessBuilder(targetFolder).withFixture(targetFolder).newServer()) { // Then HTTP.Response response = HTTP.POST( server.httpURI().toString() + "db/data/transaction/commit", quotedJson("{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}")); assertThat(response.get("results").get(0).get("data").size(), equalTo(3)); } }
@Test public void shouldTakeDirectoryLocationFromConfig() throws Exception { String expected = testDirectory.directory().getAbsolutePath(); addProperty(Configurator.RRDB_LOCATION_PROPERTY_KEY, expected); TestableRrdFactory factory = createRrdFactory(); factory.createRrdDbAndSampler(db, new NullJobScheduler()); assertThat(factory.directoryUsed, is(expected)); }
@Before public void setup() throws Throwable { clusterManager = new ClusterManager(clusterOfSize(3), dir.graphDbDir(), stringMap()); clusterManager.start(); cluster = clusterManager.getDefaultCluster(); cluster.await(ClusterManager.allSeesAllAsAvailable()); master = cluster.getMaster(); slave1 = cluster.getAnySlave(); slave2 = cluster.getAnySlave(slave1); }
@Test public void shouldCreateRrdFileInTempLocationForImpermanentDatabases() throws IOException { // Given String expected = testDirectory.directory().getAbsolutePath(); TestableRrdFactory factory = createRrdFactory(expected); // When factory.createRrdDbAndSampler(db, new NullJobScheduler()); // Then assertThat(factory.directoryUsed, is(expected)); }
private Configurator buildProperties() throws IOException { new File(test.directory().getAbsolutePath() + DIRSEP + "conf").mkdirs(); Properties databaseProperties = new Properties(); String databasePropertiesFileName = test.directory().getAbsolutePath() + DIRSEP + "conf" + DIRSEP + "neo4j.properties"; databaseProperties.store(new FileWriter(databasePropertiesFileName), null); Properties serverProperties = new Properties(); String serverPropertiesFilename = test.directory().getAbsolutePath() + DIRSEP + "conf" + DIRSEP + "neo4j-server.properties"; serverProperties.setProperty( Configurator.DATABASE_LOCATION_PROPERTY_KEY, test.directory().getAbsolutePath() + DIRSEP + "data" + DIRSEP + "graph.db"); serverProperties.setProperty( Configurator.DB_TUNING_PROPERTY_FILE_KEY, databasePropertiesFileName); serverProperties.setProperty(Configurator.NEO_SERVER_CONFIG_FILE_KEY, serverPropertiesFilename); serverProperties.store(new FileWriter(serverPropertiesFilename), null); return new PropertyFileConfigurator(new File(serverPropertiesFilename)); }
@Test public void shouldCloseUnusedWindow() throws Exception { // given String filename = new File(directory.directory(), "mapped.file").getAbsolutePath(); RandomAccessFile file = resources.add(new RandomAccessFile(filename, "rw")); MappedPersistenceWindow window = new MappedPersistenceWindow(0, 8, 16, file.getChannel(), READ_WRITE); // when boolean wasClosed = window.writeOutAndCloseIfFree(false); // then assertTrue(wasClosed); }
@Test public void shouldCreateRrdFileInDbSubdirectory() throws Exception { String storeDir = testDirectory.directory().getAbsolutePath(); db = new WrappedDatabase( (InternalAbstractGraphDatabase) new TestGraphDatabaseFactory().newEmbeddedDatabase(storeDir)); TestableRrdFactory factory = createRrdFactory(); // When factory.createRrdDbAndSampler(db, new NullJobScheduler()); // Then String rrdParent = new File(factory.directoryUsed).getParent(); assertThat(rrdParent, is(storeDir)); }
@Test public void shouldMoveAwayInvalidRrdFile() throws IOException { // Given String expected = new File(testDirectory.directory(), "rrd-test").getAbsolutePath(); addProperty(Configurator.RRDB_LOCATION_PROPERTY_KEY, expected); TestableRrdFactory factory = createRrdFactory(); createInvalidRrdFile(expected); // When RrdDbWrapper rrdDbAndSampler = factory.createRrdDbAndSampler(db, new NullJobScheduler()); // Then assertSubdirectoryExists("rrd-test-invalid", factory.directoryUsed); rrdDbAndSampler.close(); }
@Test public void shouldHandleStringFixtures() throws Exception { // Given two files in the root folder File targetFolder = testDir.directory(); // When try (ServerControls server = newInProcessBuilder(targetFolder).withFixture("CREATE (a:User)").newServer()) { // Then HTTP.Response response = HTTP.POST( server.httpURI().toString() + "db/data/transaction/commit", quotedJson("{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}")); assertThat(response.get("results").get(0).get("data").size(), equalTo(1)); } }
@Test public void shouldReadNodeRecords() throws IOException { File storeDir = dir.graphDbDir(); find19FormatHugeStoreDirectory(storeDir); Legacy19PropertyStoreReader propStoreReader = new Legacy19PropertyStoreReader(fs, new File(storeDir, "neostore.propertystore.db")); int propCount = 0; Iterator<PropertyRecord> iterator = propStoreReader.readPropertyStore(); while (iterator.hasNext()) { PropertyRecord record = iterator.next(); if (record.inUse()) { propCount++; } } assertEquals(6000, propCount); propStoreReader.close(); }
private File databaseWithUniquenessConstraint(String label, String propertyKey) { File storeDir = new File(targetDir.directory(), "seed"); GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(storeDir.getAbsolutePath()); try { Transaction tx = graphDb.beginTx(); try { graphDb.schema().constraintFor(label(label)).assertPropertyIsUnique(propertyKey).create(); tx.success(); } finally { tx.finish(); } } finally { graphDb.shutdown(); } return storeDir; }
@Test public void shouldHandleFixturesWithSyntaxErrorsGracefully() throws Exception { // Given two files in the root folder File targetFolder = testDir.directory(); FileUtils.writeToFile( new File(targetFolder, "fixture1.cyp"), "this is not a valid cypher statement", false); // When try (ServerControls ignore = newInProcessBuilder(targetFolder).withFixture(targetFolder).newServer()) { fail("Should have thrown exception"); } catch (RuntimeException e) { assertThat( e.getMessage(), equalTo( "Failed to install fixtures: Invalid input 't': expected <init> " + "(line 1, column 1)\n\"this is not a valid cypher statement\"\n ^")); } }
@Test public void shouldAccepSingleCypherFileAsFixture() throws Exception { // Given File targetFolder = testDir.directory(); File fixture = new File(targetFolder, "fixture.cyp"); FileUtils.writeToFile(fixture, "CREATE (u:User)" + "CREATE (a:OtherUser)", false); // When try (ServerControls server = newInProcessBuilder(targetFolder).withFixture(fixture).newServer()) { // Then HTTP.Response response = HTTP.POST( server.httpURI().toString() + "db/data/transaction/commit", quotedJson("{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}")); assertThat(response.status(), equalTo(200)); assertThat(response.get("results").get(0).get("data").size(), equalTo(1)); } }
private TestableRrdFactory createRrdFactory() { return new TestableRrdFactory( config, new File(testDirectory.directory(), "rrd").getAbsolutePath()); }
@Test public void testPullStorm() throws Throwable { // given ClusterManager clusterManager = new ClusterManager( ClusterManager.clusterWithAdditionalArbiters(2, 1), testDirectory.directory(), stringMap( HaSettings.pull_interval.name(), "0", HaSettings.tx_push_factor.name(), "1")); clusterManager.start(); try { ClusterManager.ManagedCluster cluster = clusterManager.getDefaultCluster(); cluster.await(ClusterManager.masterAvailable()); cluster.await(ClusterManager.masterSeesSlavesAsAvailable(1)); // Create data final HighlyAvailableGraphDatabase master = cluster.getMaster(); { Transaction tx = master.beginTx(); for (int i = 0; i < 1000; i++) { master.createNode().setProperty("foo", "bar"); } tx.success(); tx.finish(); } // Slave goes down HighlyAvailableGraphDatabase slave = cluster.getAnySlave(); ClusterManager.RepairKit repairKit = cluster.fail(slave); // Create more data for (int i = 0; i < 1000; i++) { { Transaction tx = master.beginTx(); for (int j = 0; j < 1000; j++) { master.createNode().setProperty("foo", "bar"); master.createNode().setProperty("foo", "bar"); } tx.success(); tx.finish(); } } // Slave comes back online repairKit.repair(); cluster.await(ClusterManager.masterSeesSlavesAsAvailable(1)); // when // Create 20 concurrent transactions System.out.println("Pull storm"); ExecutorService executor = Executors.newFixedThreadPool(20); for (int i = 0; i < 20; i++) { executor.submit( new Runnable() { @Override public void run() { Transaction tx = master.beginTx(); master.createNode().setProperty("foo", "bar"); tx.success(); tx.finish(); // This should cause lots of concurrent calls to pullUpdate() } }); } executor.shutdown(); executor.awaitTermination(1, TimeUnit.MINUTES); System.out.println("Pull storm done"); // then long masterLastCommittedTxId = lastCommittedTxId(master); for (HighlyAvailableGraphDatabase member : cluster.getAllMembers()) { assertEquals(masterLastCommittedTxId, lastCommittedTxId(member)); } } finally { System.err.println("Shutting down"); clusterManager.shutdown(); System.err.println("Shut down"); } }