Skip to content

Commit 2b28258

Browse files
committed
MNEMONIC-308:add test case for the init method
1 parent 09a468f commit 2b28258

File tree

4 files changed

+230
-20
lines changed

4 files changed

+230
-20
lines changed

mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImpl.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717

1818
package org.apache.mnemonic.service.memoryservice.internal;
1919

20-
import org.apache.mnemonic.service.memoryservice.VolatileMemoryAllocatorService;
2120
import org.apache.mnemonic.ConfigurationException;
22-
import org.flowcomputing.commons.primitives.NativeLibraryLoader;
21+
import org.apache.mnemonic.service.memoryservice.VolatileMemoryAllocatorService;
2322

23+
import java.io.File;
24+
import java.io.IOException;
25+
import java.io.RandomAccessFile;
2426
import java.nio.ByteBuffer;
27+
import java.nio.channels.FileChannel;
28+
import java.util.ArrayList;
2529
import java.util.Collections;
2630
import java.util.HashMap;
2731
import java.util.Map;
@@ -40,11 +44,11 @@ public String getServiceId() {
4044
public long init(long capacity, String uri, boolean isnew) {
4145
FileChannel channel = null;
4246
RandomAccessFile mappedFile = null;
43-
long cp = null;
44-
long ret = null;
47+
long cp = -1;
48+
long ret = -1;
4549

4650

47-
if (uri == null || uri.length == 0) {
51+
if (uri == null || uri.length() == 0) {
4852
throw new ConfigurationException(String.format("Please supply the file path: %s.", uri));
4953
}
5054
if (capacity <= 0) {
@@ -66,16 +70,27 @@ public long init(long capacity, String uri, boolean isnew) {
6670
throw new ConfigurationException(String.format("Failed to delete the file: %s.", uri));
6771
}
6872
}
69-
mappedFile = new RandomAccessFile(file, "rw");
70-
mappedFile.setLength(capacity);
73+
try {
74+
mappedFile = new RandomAccessFile(file, "rw");
75+
mappedFile.setLength(capacity);
76+
cp = mappedFile.length();
77+
} catch (Exception ex) { }
7178
} else {
72-
mappedFile = new RandomAccessFile(file, "rw");
79+
if (!file.exists()) {
80+
throw new ConfigurationException(String.format("File doesn't exist under the specifiled uri: %s", uri));
81+
}
82+
try {
83+
mappedFile = new RandomAccessFile(file, "rw");
84+
cp = mappedFile.length();
85+
} catch (Exception ex) { }
7386
}
7487

75-
cp = file.length();
76-
mem_pool.add(mappedFile);
77-
ret = mem_pool.length - 1;
78-
m_info.put(ret, cp);
88+
89+
if (mappedFile != null) {
90+
mem_pool.add(mappedFile);
91+
ret = mem_pool.size() - 1;
92+
m_info.put(ret, cp);
93+
}
7994

8095
return ret;
8196
}
@@ -87,9 +102,14 @@ public long adjustCapacity(long id, long reserve) {
87102

88103
@Override
89104
public void close(long id) {
90-
if (mem_pool.get(id) != null) {
91-
mem_pool.get(id).close();
92-
mem_pool.get(id) = null;
105+
int idx = (int) id;
106+
if (mem_pool.get(idx) != null) {
107+
try {
108+
mem_pool.get(idx).close();
109+
} catch (IOException e) {
110+
} finally {
111+
mem_pool.set(idx, null);
112+
}
93113
}
94114
}
95115

@@ -146,17 +166,17 @@ public void destroyByteBuffer(long id, ByteBuffer bytebuf) {
146166
@Override
147167
public ByteBuffer retrieveByteBuffer(long id, long handler) {
148168
ByteBuffer myByteBuffer = null;
149-
return myByteBuffer;//need change
169+
return myByteBuffer; //need change
150170
}
151171

152172
@Override
153173
public long retrieveSize(long id, long handler) {
154-
return 1L;//need change
174+
return 1L; //need change
155175
}
156176

157177
@Override
158178
public long getByteBufferHandler(long id, ByteBuffer buf) {
159-
return 1L;//need change
179+
return 1L; //need change
160180
}
161181

162182
@Override
@@ -176,7 +196,7 @@ public long handlerCapacity(long id) {
176196

177197
@Override
178198
public long getBaseAddress(long id) {
179-
return 1L;//need change
199+
return 1L; //need change
180200
}
181201

182202
@Override
@@ -198,4 +218,11 @@ public void abortTransaction() {
198218
public boolean isInTransaction() {
199219
throw new UnsupportedOperationException("Not support transaction");
200220
}
201-
}
221+
222+
public Map<Long, Long> getMInfo() {
223+
return this.m_info;
224+
}
225+
public ArrayList<RandomAccessFile> getMemPool() {
226+
return this.mem_pool;
227+
}
228+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.mnemonic.service.memoryservice.internal;
19+
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.Paths;
25+
26+
import org.testng.annotations.Test;
27+
import static org.testng.Assert.assertTrue;
28+
29+
/**
30+
* test the functionalities of JavaVMemServiceImpl class
31+
*
32+
*/
33+
public class JavaVMemServiceImplNGTest {
34+
private String testFile = "./jvmstest.dat";
35+
/**
36+
* test to verify the initial of memory service successes when isnew = true
37+
* regardless of whether the file exists or not (file exists)
38+
*/
39+
@Test
40+
public void testToInitByCreateNewFile1() throws IOException, ClassNotFoundException {
41+
//Ensure it doesn't impact the test file
42+
String dest = "./jvmstestCopy.dat";
43+
Files.copy(Paths.get(testFile), Paths.get(dest));
44+
45+
JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
46+
long cap = 10 * 1024 * 1024;
47+
long idx = jvms.init(10 * 1024 * 1024, dest, true);
48+
assertTrue(idx != -1);
49+
assertTrue(idx == 0);
50+
assertTrue(jvms.getMInfo().get(idx).equals(cap));
51+
jvms.close(idx);
52+
Files.delete(Paths.get(dest));
53+
}
54+
55+
/**
56+
* test to verify the initial of memory service successes when isnew = true
57+
* regardless of whether the file exists or not (file doesn't exist)
58+
*/
59+
@Test
60+
public void testToInitByCreateNewFile2() throws IOException, ClassNotFoundException {
61+
JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
62+
long cap = 10 * 1024 * 1024;
63+
long idx = jvms.init(10 * 1024 * 1024, "./jvmstestnotexist.dat", true);
64+
assertTrue(idx != -1);
65+
assertTrue(idx == 0);
66+
assertTrue(jvms.getMInfo().get(idx).equals(cap));
67+
68+
//Delete the new created test file
69+
File file = new File("./jvmstestnotexist.dat");
70+
file.delete();
71+
jvms.close(idx);
72+
}
73+
74+
75+
76+
/**
77+
* test to verify the initial of memory service fails when isnew = true
78+
* and the specifiled uri is not a file
79+
*/
80+
@Test
81+
public void testToInitFailWhenNofile() throws IOException, ClassNotFoundException {
82+
long idx = -1;
83+
boolean thrown = false;
84+
JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
85+
try {
86+
idx = jvms.init(10 * 1024 * 1024, ".", true);
87+
} catch (Exception e) {
88+
thrown = true;
89+
} finally {
90+
assertTrue(thrown);
91+
if (idx >= 0) {
92+
jvms.close(idx);
93+
}
94+
}
95+
}
96+
97+
/**
98+
* test to verify the initial of memory service successes when isnew = false
99+
* and the specifiled file exists.
100+
*/
101+
@Test
102+
public void testToInitWhenFileExists() throws IOException, ClassNotFoundException {
103+
JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
104+
File file = new File(testFile);
105+
long cap = file.length();
106+
long idx = jvms.init(10 * 1024 * 1024, testFile, false);
107+
assertTrue(idx != -1);
108+
assertTrue(idx == 0);
109+
assertTrue(jvms.getMInfo().get(idx).equals(cap));
110+
jvms.close(idx);
111+
}
112+
113+
/**
114+
* test to verify the initial of memory service fails when isnew = false
115+
* and the specifiled file doesn't exist.
116+
*/
117+
@Test
118+
public void testToInitFailWhenFileNotExists() throws IOException, ClassNotFoundException {
119+
long idx = -1;
120+
boolean thrown = false;
121+
JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
122+
try {
123+
idx = jvms.init(10 * 1024 * 1024, "./jvmstestnotexist.dat", false);
124+
} catch (Exception e) {
125+
thrown = true;
126+
} finally {
127+
assertTrue(thrown);
128+
if (idx >= 0) {
129+
jvms.close(idx);
130+
}
131+
}
132+
}
133+
134+
/**
135+
* test to verify the initial of memory service fails when isnew = false
136+
* and the specifiled uri is not a file.
137+
*/
138+
@Test
139+
public void testToInitFailWhenNotAFile() throws IOException, ClassNotFoundException {
140+
long idx = -1;
141+
boolean thrown = false;
142+
JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
143+
try {
144+
idx = jvms.init(10 * 1024 * 1024, ".", false);
145+
} catch (Exception e) {
146+
thrown = true;
147+
} finally {
148+
assertTrue(thrown);
149+
if (idx >= 0) {
150+
jvms.close(idx);
151+
}
152+
}
153+
}
154+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<suite name="Mnenomic Memory Service test Suite" verbose="1" parallel="tests" thread-count="1">
23+
<test name="Java Memory Service" >
24+
<classes>
25+
<class name="org.apache.mnemonic.service.memoryservice.internal.JavaVMemServiceImplNGTest" />
26+
</classes>
27+
</test>
28+
</suite>

mnemonic-memory-services/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<module>mnemonic-nvml-pmem-service</module>
4444
<module>mnemonic-pmalloc-service</module>
4545
<module>mnemonic-sys-vmem-service</module>
46+
<module>mnemonic-java-vmem-service</module>
4647
</modules>
4748

4849
<dependencies>

0 commit comments

Comments
 (0)