You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mnemonic.apache.org by ga...@apache.org on 2017/07/22 02:40:10 UTC

incubator-mnemonic git commit: MNEMONIC-308:add test case for the init method

Repository: incubator-mnemonic
Updated Branches:
  refs/heads/master 09a468f69 -> 2b2825825


MNEMONIC-308:add test case for the init method


Project: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/commit/2b282582
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/tree/2b282582
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/diff/2b282582

Branch: refs/heads/master
Commit: 2b282582530ace0909e9a519e0709dc36e19e17e
Parents: 09a468f
Author: paleyss <pa...@gmail.com>
Authored: Fri Jul 21 17:22:46 2017 -0700
Committer: paleyss <pa...@gmail.com>
Committed: Fri Jul 21 17:22:46 2017 -0700

----------------------------------------------------------------------
 .../internal/JavaVMemServiceImpl.java           |  67 +++++---
 .../internal/JavaVMemServiceImplNGTest.java     | 154 +++++++++++++++++++
 .../src/test/resources/testng.xml               |  28 ++++
 mnemonic-memory-services/pom.xml                |   1 +
 4 files changed, 230 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/2b282582/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImpl.java
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImpl.java b/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImpl.java
index 615d901..cb34c06 100644
--- a/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImpl.java
+++ b/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImpl.java
@@ -17,11 +17,15 @@
 
 package org.apache.mnemonic.service.memoryservice.internal;
 
-import org.apache.mnemonic.service.memoryservice.VolatileMemoryAllocatorService;
 import org.apache.mnemonic.ConfigurationException;
-import org.flowcomputing.commons.primitives.NativeLibraryLoader;
+import org.apache.mnemonic.service.memoryservice.VolatileMemoryAllocatorService;
 
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
@@ -40,11 +44,11 @@ public class JavaVMemServiceImpl implements VolatileMemoryAllocatorService {
   public long init(long capacity, String uri, boolean isnew) {
     FileChannel channel = null;
     RandomAccessFile mappedFile = null;
-    long cp = null;
-    long ret = null;
+    long cp = -1;
+    long ret = -1;
 
 
-    if (uri == null || uri.length == 0) {
+    if (uri == null || uri.length() == 0) {
       throw new ConfigurationException(String.format("Please supply the file path: %s.", uri));
     }
     if (capacity <= 0) {
@@ -66,16 +70,27 @@ public class JavaVMemServiceImpl implements VolatileMemoryAllocatorService {
           throw new ConfigurationException(String.format("Failed to delete the file: %s.", uri));
         }
       }
-      mappedFile = new RandomAccessFile(file, "rw");
-      mappedFile.setLength(capacity);
+      try {
+        mappedFile = new RandomAccessFile(file, "rw");
+        mappedFile.setLength(capacity);
+        cp = mappedFile.length();
+      } catch (Exception ex) { }
     } else {
-      mappedFile = new RandomAccessFile(file, "rw");
+      if (!file.exists()) {
+        throw new ConfigurationException(String.format("File doesn't exist under the specifiled uri: %s", uri));
+      }
+      try {
+        mappedFile = new RandomAccessFile(file, "rw");
+        cp = mappedFile.length();
+      } catch (Exception ex) { }
     }
 
-    cp = file.length();
-    mem_pool.add(mappedFile);
-    ret = mem_pool.length - 1;
-    m_info.put(ret, cp);
+
+    if (mappedFile != null) {
+      mem_pool.add(mappedFile);
+      ret = mem_pool.size() - 1;
+      m_info.put(ret, cp);
+    }
 
     return ret;
   }
@@ -87,9 +102,14 @@ public class JavaVMemServiceImpl implements VolatileMemoryAllocatorService {
 
   @Override
   public void close(long id) {
-    if (mem_pool.get(id) != null) {
-      mem_pool.get(id).close();
-      mem_pool.get(id) = null;
+    int idx = (int) id;
+    if (mem_pool.get(idx) != null) {
+      try {
+        mem_pool.get(idx).close();
+      } catch (IOException e) {
+      } finally {
+        mem_pool.set(idx, null);
+      }
     }
   }
 
@@ -146,17 +166,17 @@ public class JavaVMemServiceImpl implements VolatileMemoryAllocatorService {
   @Override
   public ByteBuffer retrieveByteBuffer(long id, long handler) {
     ByteBuffer myByteBuffer = null;
-    return myByteBuffer;//need change
+    return myByteBuffer; //need change
   }
 
   @Override
   public long retrieveSize(long id, long handler) {
-    return 1L;//need change
+    return 1L; //need change
   }
 
   @Override
   public long getByteBufferHandler(long id, ByteBuffer buf) {
-    return 1L;//need change
+    return 1L; //need change
   }
 
   @Override
@@ -176,7 +196,7 @@ public class JavaVMemServiceImpl implements VolatileMemoryAllocatorService {
 
   @Override
   public long getBaseAddress(long id) {
-    return 1L;//need change
+    return 1L; //need change
   }
 
   @Override
@@ -198,4 +218,11 @@ public class JavaVMemServiceImpl implements VolatileMemoryAllocatorService {
   public boolean isInTransaction() {
     throw new UnsupportedOperationException("Not support transaction");
   }
-}
\ No newline at end of file
+
+  public Map<Long, Long> getMInfo() {
+    return this.m_info;
+  }
+  public ArrayList<RandomAccessFile> getMemPool() {
+    return this.mem_pool;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/2b282582/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImplNGTest.java
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImplNGTest.java b/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImplNGTest.java
new file mode 100644
index 0000000..d923954
--- /dev/null
+++ b/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImplNGTest.java
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mnemonic.service.memoryservice.internal;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import org.testng.annotations.Test;
+import static org.testng.Assert.assertTrue;
+
+/**
+ * test the functionalities of JavaVMemServiceImpl class
+ * 
+ */
+public class JavaVMemServiceImplNGTest {
+   private String testFile = "./jvmstest.dat";
+  /**
+   * test to verify the initial of memory service successes when isnew = true
+   * regardless of whether the file exists or not (file exists)
+   */
+  @Test
+  public void testToInitByCreateNewFile1() throws IOException, ClassNotFoundException {
+    //Ensure it doesn't impact the test file
+    String dest = "./jvmstestCopy.dat";
+    Files.copy(Paths.get(testFile), Paths.get(dest));
+
+    JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+    long cap = 10 * 1024 * 1024;
+    long idx = jvms.init(10 * 1024 * 1024, dest, true);
+    assertTrue(idx != -1);
+    assertTrue(idx == 0);
+    assertTrue(jvms.getMInfo().get(idx).equals(cap));
+    jvms.close(idx);
+    Files.delete(Paths.get(dest));
+  }
+
+  /**
+   * test to verify the initial of memory service successes when isnew = true
+   * regardless of whether the file exists or not (file doesn't exist)
+   */
+  @Test
+  public void testToInitByCreateNewFile2() throws IOException, ClassNotFoundException {
+    JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+    long cap = 10 * 1024 * 1024;
+    long idx = jvms.init(10 * 1024 * 1024, "./jvmstestnotexist.dat", true);
+    assertTrue(idx != -1);
+    assertTrue(idx == 0);
+    assertTrue(jvms.getMInfo().get(idx).equals(cap));
+
+    //Delete the new created test file
+    File file = new File("./jvmstestnotexist.dat");
+    file.delete();
+    jvms.close(idx);
+  }
+
+
+
+  /**
+   * test to verify the initial of memory service fails when isnew = true
+   * and the specifiled uri is not a file
+   */
+  @Test
+  public void testToInitFailWhenNofile() throws IOException, ClassNotFoundException {
+    long idx = -1;
+    boolean thrown = false;
+    JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+    try {
+      idx = jvms.init(10 * 1024 * 1024, ".", true);
+    } catch (Exception e) {
+      thrown = true;
+    } finally {
+      assertTrue(thrown);
+      if (idx >= 0) {
+        jvms.close(idx);
+      }
+    }
+  }
+
+  /**
+   * test to verify the initial of memory service successes when isnew = false
+   * and the specifiled file exists.
+   */
+  @Test
+  public void testToInitWhenFileExists() throws IOException, ClassNotFoundException {
+    JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+    File file = new File(testFile);
+    long cap = file.length();
+    long idx = jvms.init(10 * 1024 * 1024, testFile, false);
+    assertTrue(idx != -1);
+    assertTrue(idx == 0);
+    assertTrue(jvms.getMInfo().get(idx).equals(cap));
+    jvms.close(idx);
+  }
+
+  /**
+   * test to verify the initial of memory service fails when isnew = false
+   * and the specifiled file doesn't exist.
+   */
+  @Test
+  public void testToInitFailWhenFileNotExists() throws IOException, ClassNotFoundException {
+    long idx = -1;
+    boolean thrown = false;
+    JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+    try {
+      idx = jvms.init(10 * 1024 * 1024, "./jvmstestnotexist.dat", false);
+    } catch (Exception e) {
+      thrown = true;
+    } finally {
+      assertTrue(thrown);
+      if (idx >= 0) {
+        jvms.close(idx);
+      }
+    }
+  }
+
+  /**
+   * test to verify the initial of memory service fails when isnew = false
+   * and the specifiled uri is not a file.
+   */
+  @Test
+  public void testToInitFailWhenNotAFile() throws IOException, ClassNotFoundException {
+    long idx = -1;
+    boolean thrown = false;
+    JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+    try {
+      idx = jvms.init(10 * 1024 * 1024, ".", false);
+    } catch (Exception e) {
+      thrown = true;
+    } finally {
+      assertTrue(thrown);
+      if (idx >= 0) {
+        jvms.close(idx);
+      }
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/2b282582/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/resources/testng.xml
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/resources/testng.xml b/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/resources/testng.xml
new file mode 100644
index 0000000..e0279cb
--- /dev/null
+++ b/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/resources/testng.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+
+<suite name="Mnenomic Memory Service test Suite" verbose="1" parallel="tests" thread-count="1">
+    <test name="Java Memory Service" >
+        <classes>
+            <class name="org.apache.mnemonic.service.memoryservice.internal.JavaVMemServiceImplNGTest" />
+        </classes>
+    </test>
+</suite>

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/2b282582/mnemonic-memory-services/pom.xml
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/pom.xml b/mnemonic-memory-services/pom.xml
index 91353ff..47055b3 100644
--- a/mnemonic-memory-services/pom.xml
+++ b/mnemonic-memory-services/pom.xml
@@ -43,6 +43,7 @@
     <module>mnemonic-nvml-pmem-service</module>
     <module>mnemonic-pmalloc-service</module>
     <module>mnemonic-sys-vmem-service</module>
+    <module>mnemonic-java-vmem-service</module>
   </modules>
 
   <dependencies>