Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 Ursa Project LLC ( Licensed 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 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 com.ursaj.hfs.gc;
 import java.util.UUID;
 import static com.ursaj.hfs.HfsTestUtils.RND;
 import static com.ursaj.hfs.HfsTestUtils.assertIllegalArgument;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;

 public class HfsGcResponseWriterTest {
Test input validations.

Throws: In case of any IO exception.
     public void testValidations() throws IOException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         HfsGcResponseWriter out = new HfsGcResponseWriter(baos);
         final long p = .nextLong() & .;
         final long q = .nextLong() & .;
         for (int i = 0; i < 100; i++) {
             assertIllegalArgument("cannot be null"HfsGcResponseWriter.class"newInstance", (OutputStreamnull);
             for (int j = 0; j < 100; j++)
                 assertTrue(out.offer(new UUID(p + iq + j)));
             for (int k = 0; k <= ik++)
                 assertIllegalArgument("should follow in natural (unsigned) ordering",
                     out"offer"new UUID(p + kq + 99));
         ByteBuffer data = ByteBuffer.wrap(baos.toByteArray());
         byte[] expected = .;
         byte[] header = new byte[expected.length];
         for (int i = 0; i < 100; i++) {
             for (int j = 0; j < 100; j++) {
                 UUID fileId = HfsGcResponseWriter.readUUID(data);
                 short integrity = data.getShort();
                 assertEquals(fileIdnew UUID(p + iq + j));
                 int hashCode = fileId.hashCode();
                 assertEquals(integrity, (short) ((hashCode >> 16) ^ hashCode));

Test multi-threaded access to GC response stream.

java.lang.Exception In case of any exception.
     public void testMultiThreaded() throws Exception {
         final HfsGcResponseWriter out = new HfsGcResponseWriter(new OutputStream() {
             public void write(int bthrows IOException {
                 // No-op.
         ViolationsCalculator c = new ViolationsCalculator(out, 20000);
        HfsTestUtils.multithreaded(50, c);
        assertTrue(c.caughtOrdering.get() > 50, "Ordering violations count: " + c.caughtOrdering);
        assertTrue(c.caughtConcurrency.get() > 50, "Concurrency violations count: " + c.caughtConcurrency);

Concurrent write violations calculator.
    private static final class ViolationsCalculator implements com.ursaj.hfs.HfsRunnable {
Total iterations counter.
        private final AtomicLong counter = new AtomicLong();

Caught ordering violations counter.
        private final AtomicLong caughtOrdering = new AtomicLong();

Caught concurrency violations counter.
        private final AtomicLong caughtConcurrency = new AtomicLong();

GC response stream to write file IDs to.
        private final HfsGcResponseWriter out;

Total number of iterations.
        private final long total;

Constructs violations calculator.

out GC response stream to write file IDs to.
total Total number of iterations.
        private ViolationsCalculator(HfsGcResponseWriter outlong total) {
            this. = out;
            this. = total;
        public void run() throws IOException {
            for (long i = .incrementAndGet(); i < i = .incrementAndGet()) {
                try {
                    Thread.yield(); // Pause this thread.
                    assertTrue(.offer(new UUID(0, i)));
                catch (IllegalArgumentException e) {
                    if (e.getMessage().contains("should follow in natural (unsigned) ordering"))
                        throw e;
                catch (IllegalStateException e) {
                    if (e.getMessage().contains("write IDs concurrently"))
                        throw e;
New to GrepCode? Check out our FAQ X