samples/reentrant/test/org/apidesign/reentrant/CriticalSectionBase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:54:36 +0200
changeset 111 3905a2e66b9b
child 112 64308321f7bd
permissions -rw-r--r--
Sample code with various attempts to fight with reentrant code
jtulach@111
     1
package org.apidesign.reentrant;
jtulach@111
     2
jtulach@111
     3
import java.util.ArrayList;
jtulach@111
     4
import java.util.Iterator;
jtulach@111
     5
import java.util.List;
jtulach@111
     6
import org.junit.Test;
jtulach@111
     7
import static org.junit.Assert.*;
jtulach@111
     8
jtulach@111
     9
public abstract class CriticalSectionBase {
jtulach@111
    10
    protected abstract CriticalSection<Integer> create();
jtulach@111
    11
jtulach@111
    12
    @Test
jtulach@111
    13
    public void testCriticalSectionWith15() {
jtulach@111
    14
        final CriticalSection<Integer> cs = create();
jtulach@111
    15
        testFor15(cs);
jtulach@111
    16
    }
jtulach@111
    17
    
jtulach@111
    18
    final void testFor15(CriticalSection<Integer> cs) {
jtulach@111
    19
        cs.assignPilot(15);
jtulach@111
    20
jtulach@111
    21
        List<Integer> ints = new ArrayList<Integer>();
jtulach@111
    22
        ints.add(8);
jtulach@111
    23
        ints.add(11);
jtulach@111
    24
        ints.add(10);
jtulach@111
    25
        ints.add(5);
jtulach@111
    26
        ints.add(12);
jtulach@111
    27
        ints.add(18);
jtulach@111
    28
        ints.add(13);
jtulach@111
    29
        ints.add(7);
jtulach@111
    30
jtulach@111
    31
        int cnt = cs.sumBigger(ints);
jtulach@111
    32
jtulach@111
    33
        assertEquals("18 is bigger than 15", 1, cnt);
jtulach@111
    34
        
jtulach@111
    35
    }
jtulach@111
    36
    
jtulach@111
    37
    @Test 
jtulach@111
    38
    public void teaseCriticalSectionWithReentrantCalls() {
jtulach@111
    39
        final CriticalSection<Integer> cs = create();
jtulach@111
    40
        
jtulach@111
    41
        cs.assignPilot(10);
jtulach@111
    42
        
jtulach@111
    43
        class ChangePilotTo15 implements Runnable {
jtulach@111
    44
            public void run() {
jtulach@111
    45
                testFor15(cs);
jtulach@111
    46
            }
jtulach@111
    47
        }
jtulach@111
    48
        
jtulach@111
    49
        List<Integer> ints = new MyCollection(new ChangePilotTo15(), 3);
jtulach@111
    50
        ints.add(8);
jtulach@111
    51
        ints.add(11);
jtulach@111
    52
        ints.add(10);
jtulach@111
    53
        ints.add(5);
jtulach@111
    54
        ints.add(12);
jtulach@111
    55
        ints.add(18);
jtulach@111
    56
        ints.add(13);
jtulach@111
    57
        ints.add(7);
jtulach@111
    58
        
jtulach@111
    59
        int cnt = cs.sumBigger(ints);
jtulach@111
    60
        
jtulach@111
    61
        assertEquals("11, 12, 18, 13 are bigger than 10", 4, cnt);
jtulach@111
    62
        
jtulach@111
    63
        assertEquals("Global count is sum of ints(e.g. 4) plus result of testFor15(e.g. 1)", 5, cs.getCount());
jtulach@111
    64
    }
jtulach@111
    65
jtulach@111
    66
    class MyCollection extends ArrayList<Integer> {
jtulach@111
    67
        private final Runnable callback;
jtulach@111
    68
        private final int callbackBeforeIndex;
jtulach@111
    69
jtulach@111
    70
        public MyCollection(Runnable callback, int callbackAtIndex) {
jtulach@111
    71
            this.callback = callback;
jtulach@111
    72
            this.callbackBeforeIndex = callbackAtIndex;
jtulach@111
    73
        }
jtulach@111
    74
jtulach@111
    75
        @Override
jtulach@111
    76
        public Iterator<Integer> iterator() {
jtulach@111
    77
            final Iterator<Integer> delegate = super.iterator();
jtulach@111
    78
            class It implements Iterator<Integer> {
jtulach@111
    79
                private int index;
jtulach@111
    80
                
jtulach@111
    81
                public boolean hasNext() {
jtulach@111
    82
                    return delegate.hasNext();
jtulach@111
    83
                }
jtulach@111
    84
jtulach@111
    85
                public Integer next() {
jtulach@111
    86
                    if (index++ == callbackBeforeIndex) {
jtulach@111
    87
                        callback.run();
jtulach@111
    88
                    }
jtulach@111
    89
                    
jtulach@111
    90
                    return delegate.next();
jtulach@111
    91
                }
jtulach@111
    92
jtulach@111
    93
                public void remove() {
jtulach@111
    94
                    delegate.remove();
jtulach@111
    95
                }
jtulach@111
    96
            } // end of It
jtulach@111
    97
            
jtulach@111
    98
            return new It();
jtulach@111
    99
        }
jtulach@111
   100
    }
jtulach@111
   101
}