samples/reentrant/test/org/apidesign/reentrant/CriticalSectionBase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:54:37 +0200
changeset 112 64308321f7bd
parent 111 3905a2e66b9b
child 263 7e8e995065c5
permissions -rw-r--r--
Reentrant access examples rewritten to make a bit more sence
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@112
    11
    protected boolean reentrantJustOnce() {
jtulach@112
    12
        return false;
jtulach@112
    13
    }
jtulach@111
    14
jtulach@112
    15
    // BEGIN: reentrant.ok.call
jtulach@111
    16
    @Test
jtulach@111
    17
    public void testCriticalSectionWith15() {
jtulach@111
    18
        final CriticalSection<Integer> cs = create();
jtulach@111
    19
        testFor15(cs);
jtulach@111
    20
    }
jtulach@112
    21
    // END: reentrant.ok.call
jtulach@111
    22
    
jtulach@111
    23
    final void testFor15(CriticalSection<Integer> cs) {
jtulach@111
    24
        cs.assignPilot(15);
jtulach@111
    25
jtulach@111
    26
        List<Integer> ints = new ArrayList<Integer>();
jtulach@111
    27
        ints.add(8);
jtulach@111
    28
        ints.add(11);
jtulach@111
    29
        ints.add(10);
jtulach@111
    30
        ints.add(5);
jtulach@111
    31
        ints.add(12);
jtulach@111
    32
        ints.add(18);
jtulach@111
    33
        ints.add(13);
jtulach@111
    34
        ints.add(7);
jtulach@111
    35
jtulach@111
    36
        int cnt = cs.sumBigger(ints);
jtulach@111
    37
jtulach@111
    38
        assertEquals("18 is bigger than 15", 1, cnt);
jtulach@111
    39
        
jtulach@111
    40
    }
jtulach@111
    41
    
jtulach@111
    42
    @Test 
jtulach@111
    43
    public void teaseCriticalSectionWithReentrantCalls() {
jtulach@111
    44
        final CriticalSection<Integer> cs = create();
jtulach@111
    45
        
jtulach@111
    46
        cs.assignPilot(10);
jtulach@111
    47
        
jtulach@111
    48
        class ChangePilotTo15 implements Runnable {
jtulach@112
    49
            // BEGIN: reentrant.forbidden.call
jtulach@112
    50
            // if this runnable is called from inside the critical section,
jtulach@112
    51
            // and the locks are non-reentrant then it throws an exception
jtulach@111
    52
            public void run() {
jtulach@111
    53
                testFor15(cs);
jtulach@111
    54
            }
jtulach@112
    55
            // END: reentrant.forbidden.call
jtulach@111
    56
        }
jtulach@111
    57
        
jtulach@111
    58
        List<Integer> ints = new MyCollection(new ChangePilotTo15(), 3);
jtulach@111
    59
        ints.add(8);
jtulach@111
    60
        ints.add(11);
jtulach@111
    61
        ints.add(10);
jtulach@111
    62
        ints.add(5);
jtulach@111
    63
        ints.add(12);
jtulach@111
    64
        ints.add(18);
jtulach@111
    65
        ints.add(13);
jtulach@111
    66
        ints.add(7);
jtulach@111
    67
        
jtulach@111
    68
        int cnt = cs.sumBigger(ints);
jtulach@111
    69
        
jtulach@111
    70
        assertEquals("11, 12, 18, 13 are bigger than 10", 4, cnt);
jtulach@111
    71
        
jtulach@111
    72
        assertEquals("Global count is sum of ints(e.g. 4) plus result of testFor15(e.g. 1)", 5, cs.getCount());
jtulach@111
    73
    }
jtulach@111
    74
jtulach@111
    75
    class MyCollection extends ArrayList<Integer> {
jtulach@112
    76
        private Runnable callback;
jtulach@111
    77
        private final int callbackBeforeIndex;
jtulach@111
    78
jtulach@111
    79
        public MyCollection(Runnable callback, int callbackAtIndex) {
jtulach@111
    80
            this.callback = callback;
jtulach@111
    81
            this.callbackBeforeIndex = callbackAtIndex;
jtulach@111
    82
        }
jtulach@111
    83
jtulach@111
    84
        @Override
jtulach@111
    85
        public Iterator<Integer> iterator() {
jtulach@111
    86
            final Iterator<Integer> delegate = super.iterator();
jtulach@111
    87
            class It implements Iterator<Integer> {
jtulach@111
    88
                private int index;
jtulach@111
    89
                
jtulach@111
    90
                public boolean hasNext() {
jtulach@111
    91
                    return delegate.hasNext();
jtulach@111
    92
                }
jtulach@111
    93
jtulach@111
    94
                public Integer next() {
jtulach@111
    95
                    if (index++ == callbackBeforeIndex) {
jtulach@112
    96
                        if (callback != null) {
jtulach@112
    97
                            callback.run();
jtulach@112
    98
                        }
jtulach@112
    99
                        if (reentrantJustOnce()) {
jtulach@112
   100
                            callback = null;
jtulach@112
   101
                        }
jtulach@111
   102
                    }
jtulach@111
   103
                    
jtulach@111
   104
                    return delegate.next();
jtulach@111
   105
                }
jtulach@111
   106
jtulach@111
   107
                public void remove() {
jtulach@111
   108
                    delegate.remove();
jtulach@111
   109
                }
jtulach@111
   110
            } // end of It
jtulach@111
   111
            
jtulach@111
   112
            return new It();
jtulach@111
   113
        }
jtulach@111
   114
    }
jtulach@111
   115
}