~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to src/test/java/net/sf/ehcache/transaction/xa/TwoPCTest.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
import net.sf.ehcache.Element;
10
10
import net.sf.ehcache.config.CacheConfiguration;
11
11
import net.sf.ehcache.config.Configuration;
 
12
import net.sf.ehcache.transaction.TransactionTimeoutException;
12
13
import org.junit.After;
13
14
import org.junit.Before;
14
15
import org.junit.Test;
15
16
 
 
17
import java.lang.reflect.Field;
 
18
import java.util.Map;
 
19
 
 
20
import javax.transaction.RollbackException;
16
21
import javax.transaction.Status;
17
22
 
 
23
import static org.junit.Assert.assertEquals;
 
24
import static org.junit.Assert.fail;
 
25
 
18
26
/**
19
27
 * @author lorban
20
28
 */
39
47
                .maxEntriesLocalHeap(10)
40
48
                .transactionalMode(CacheConfiguration.TransactionalMode.XA_STRICT)));
41
49
 
42
 
        transactionManager.begin();
43
50
        xaCache1 = cacheManager.getEhcache("xaCache1");
 
51
        xaCache2 = cacheManager.getEhcache("xaCache2");
 
52
    }
 
53
 
 
54
    private void clearAll() throws Exception {
 
55
        transactionManager.begin();
44
56
        xaCache1.removeAll();
45
 
        xaCache2 = cacheManager.getEhcache("xaCache2");
46
57
        xaCache2.removeAll();
47
58
        transactionManager.commit();
48
59
    }
58
69
 
59
70
    @Test
60
71
    public void testRemoveCachesAfterPhase1() throws Exception {
 
72
        clearAll();
61
73
        transactionManager.begin();
62
74
 
63
75
        for (int i = 0; i < 100; i++) {
87
99
        transactionManager.commit();
88
100
    }
89
101
 
90
 
}
 
102
    @Test
 
103
    public void testTimeout() throws Exception {
 
104
        xaCache1.getCacheManager().getTransactionController().setDefaultTransactionTimeout(5);
 
105
 
 
106
        transactionManager.setTransactionTimeout(2);
 
107
        transactionManager.begin();
 
108
 
 
109
        // get doesn't enlist -> XATransactionStore will set this cache's XA timeout for this TX to be the default local TX timeout
 
110
        xaCache1.get(1);
 
111
 
 
112
        Thread.sleep(3000);
 
113
        // XA tx timed out but the resource doesn't know about it -> call succeeds
 
114
        xaCache1.get(1);
 
115
 
 
116
        Thread.sleep(3000);
 
117
        // cache's XA timeout for this TX reached -> fail
 
118
        try {
 
119
            xaCache1.get(1);
 
120
            fail("expected TransactionTimeoutException");
 
121
        } catch (TransactionTimeoutException e) {
 
122
            // expected
 
123
        }
 
124
 
 
125
        try {
 
126
            // this XA tx timed out
 
127
            transactionManager.commit();
 
128
            fail("expected RollbackException");
 
129
        } catch (RollbackException e) {
 
130
            // expected
 
131
        }
 
132
 
 
133
        // check that there is no internal leak (EHC-937)
 
134
        assertEquals(0, ((Map)getStoreField(xaCache1, "transactionToTimeoutMap")).size());
 
135
    }
 
136
 
 
137
    private static Object getStoreField(Ehcache cache, String storeFieldName) throws IllegalAccessException, NoSuchFieldException {
 
138
        Field storeField = cache.getClass().getDeclaredField("compoundStore");
 
139
        storeField.setAccessible(true);
 
140
        XATransactionStore store = (XATransactionStore)storeField.get(cache);
 
141
 
 
142
        Field field = store.getClass().getDeclaredField(storeFieldName);
 
143
        field.setAccessible(true);
 
144
        return field.get(store);
 
145
    }
 
146
 
 
147
}
 
 
b'\\ No newline at end of file'