From 912d1706e75c8d6620864ba72ff3eef415aa6c2b Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 26 Aug 2020 08:50:21 -0700 Subject: [PATCH] wip --- .../mmap => seaweedfs/file}/MmapFileTest.java | 4 +- .../seaweedfs/file/RandomeAccessFileTest.java | 70 +++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) rename test/random_access/src/test/java/{seaewedfs/mmap => seaweedfs/file}/MmapFileTest.java (98%) create mode 100644 test/random_access/src/test/java/seaweedfs/file/RandomeAccessFileTest.java diff --git a/test/random_access/src/test/java/seaewedfs/mmap/MmapFileTest.java b/test/random_access/src/test/java/seaweedfs/file/MmapFileTest.java similarity index 98% rename from test/random_access/src/test/java/seaewedfs/mmap/MmapFileTest.java rename to test/random_access/src/test/java/seaweedfs/file/MmapFileTest.java index e64fb85f0..1d741ee2f 100644 --- a/test/random_access/src/test/java/seaewedfs/mmap/MmapFileTest.java +++ b/test/random_access/src/test/java/seaweedfs/file/MmapFileTest.java @@ -1,4 +1,4 @@ -package seaewedfs.mmap; +package seaweedfs.file; import org.junit.Test; @@ -11,7 +11,7 @@ import java.nio.channels.FileChannel; public class MmapFileTest { - File dir = new File("/Users/chris/tmp/mm/dev"); + static File dir = new File("/Users/chris/tmp/mm/dev"); @Test public void testMmap() { diff --git a/test/random_access/src/test/java/seaweedfs/file/RandomeAccessFileTest.java b/test/random_access/src/test/java/seaweedfs/file/RandomeAccessFileTest.java new file mode 100644 index 000000000..cb5847567 --- /dev/null +++ b/test/random_access/src/test/java/seaweedfs/file/RandomeAccessFileTest.java @@ -0,0 +1,70 @@ +package seaweedfs.file; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.util.Random; + +public class RandomeAccessFileTest { + + @Test + public void testRandomWriteAndRead() throws IOException { + + File f = new File(MmapFileTest.dir, "mmap_file.txt"); + + RandomAccessFile af = new RandomAccessFile(f, "rw"); + af.setLength(0); + af.close(); + + Random r = new Random(); + + int maxLength = 5000; + + byte[] data = new byte[maxLength]; + byte[] readData = new byte[maxLength]; + + for (int i = 4096; i < maxLength; i++) { + + RandomAccessFile raf = new RandomAccessFile(f, "rw"); + long fileSize = raf.length(); + + raf.readFully(readData, 0, (int)fileSize); + + for (int x=0;x stop) { + int t = stop; + stop = start; + start = t; + } + if (stop > fileSize) { + fileSize = stop; + raf.setLength(fileSize); + } + + randomize(r, data, start, stop); + raf.seek(start); + raf.write(data, start, stop-start); + + raf.close(); + } + + } + + private static void randomize(Random r, byte[] bytes, int start, int stop) { + for (int i = start; i < stop; i++) { + int rnd = r.nextInt(); + bytes[i] = (byte) rnd; + } + } + + +}