• Views
  • Iteration Report
  • My Iteration Report
  •  
OMERO.server
  • Login
  • Help/Guide
  • About Trac
  • Preferences
  • Wiki
  • Timeline
  • Roadmap
  • Browse Source
  • View Tickets
  • Search

Context Navigation

  • ← Previous Change
  • Next Change →

Changeset 2592 for trunk/components/server/test/ome/server/utests/sessions/SessMgrUnitTest.java

Show
Ignore:
Timestamp:
07/05/08 22:16:04 (5 months ago)
Author:
jmoore
Message:

ticket:1018 - Initial version of OmeroSessions reference counting

Implementation is incomplete in the blitz case since there are two
possible "closers" - the user and the glacier session, which must
be taken into account.

Files:
1 modified

  • trunk/components/server/test/ome/server/utests/sessions/SessMgrUnitTest.java (modified) (11 diffs)

Legend:

Unmodified
Added
Removed
  • trunk/components/server/test/ome/server/utests/sessions/SessMgrUnitTest.java

    r2126 r2592  
    1010import java.util.List; 
    1111 
    12 import ome.conditions.ApiUsageException; 
     12import net.sf.ehcache.CacheManager; 
     13import ome.api.local.LocalAdmin; 
     14import ome.api.local.LocalQuery; 
     15import ome.api.local.LocalUpdate; 
     16import ome.conditions.AuthenticationException; 
    1317import ome.conditions.SecurityViolation; 
    1418import ome.conditions.SessionException; 
    … …  
    2024import ome.model.meta.ExperimenterGroup; 
    2125import ome.model.meta.Session; 
     26import ome.services.sessions.SessionContext; 
    2227import ome.services.sessions.SessionManagerImpl; 
     28import ome.services.sessions.state.SessionCache; 
     29import ome.services.util.Executor; 
     30import ome.system.OmeroContext; 
    2331import ome.system.Principal; 
    2432import ome.system.Roles; 
     33import ome.testing.MockServiceFactory; 
    2534 
    2635import org.jmock.Mock; 
    2736import org.jmock.MockObjectTestCase; 
    2837import org.jmock.core.Constraint; 
     38import org.jmock.core.Invocation; 
     39import org.jmock.core.Stub; 
    2940import org.testng.annotations.BeforeTest; 
    3041import org.testng.annotations.Test; 
    … …  
    3647public class SessMgrUnitTest extends MockObjectTestCase { 
    3748 
    38     private static class TestSessionManager extends SessionManagerImpl { 
    39  
    40     } 
    41  
    42     private Mock adminMock, updateMock, queryMock; 
    43     private TestSessionManager mgr; 
    44     private Session session; 
    45  
    46     @BeforeTest 
    47     public void config() { 
    48         mgr = new TestSessionManager(); 
    49         mgr.setRoles(new Roles()); 
    50  
    51         session = new Session(); 
    52         session.setUuid("uuid"); 
    53         session.setId(1L); 
    54     } 
    55  
     49    private final class DoWorkStub implements Stub { 
     50        public Object invoke(Invocation i) throws Throwable { 
     51            Executor.Work work = (Executor.Work) i.parameterValues.get(1); 
     52            return work.doWork(null, null, sf); 
     53        } 
     54 
     55        public StringBuffer describeTo(StringBuffer sb) { 
     56            sb.append("calls doWork on work"); 
     57            return sb; 
     58        } 
     59    } 
     60 
     61    private final class TestManager extends SessionManagerImpl { 
     62        Session doDefine() { 
     63            return define("uuid", "message", System.currentTimeMillis(), 
     64                    defaultTimeToIdle, defaultTimeToLive, "Test", "rw----"); 
     65        } 
     66    } 
     67 
     68    private final OmeroContext oc = new OmeroContext( 
     69            "classpath:ome/testing/empty.xml"); 
     70    private final MockServiceFactory sf = new MockServiceFactory(); 
     71    private Mock exMock; 
     72    private TestManager mgr; 
     73    private SessionCache cache; 
     74 
     75    // State 
     76    Session session = new Session(); 
    5677    Principal principal = new Principal("u", "g", "Test"); 
    5778    String credentials = "password"; 
    … …  
    6283    List<String> userRoles = Collections.singletonList("single"); 
    6384 
     85    @BeforeTest 
     86    public void config() { 
     87 
     88        exMock = mock(Executor.class); 
     89        sf.mockAdmin = mock(LocalAdmin.class); 
     90        sf.mockUpdate = mock(LocalUpdate.class); 
     91        sf.mockQuery = mock(LocalQuery.class); 
     92 
     93        cache = new SessionCache(); 
     94        cache.setCacheManager(CacheManager.getInstance()); 
     95 
     96        mgr = new TestManager(); 
     97        mgr.setRoles(new Roles()); 
     98        mgr.setSessionCache(cache); 
     99        mgr.setExecutor((Executor) exMock.proxy()); 
     100        mgr.setApplicationContext(oc); 
     101        mgr.setDefaultTimeToIdle(100 * 1000L); 
     102        mgr.setDefaultTimeToLive(300 * 1000L); 
     103 
     104        session = mgr.doDefine(); 
     105        session.setId(1L); 
     106 
     107        user.setOmeName(principal.getName()); 
     108    } 
     109 
    64110    void prepareSessionCreation() { 
    65         adminMock.expects(once()).method("checkPassword").will( 
     111 
     112        exMock.expects(atLeastOnce()).method("execute").will(new DoWorkStub()); 
     113 
     114        sf.mockAdmin.expects(once()).method("checkPassword").will( 
    66115                returnValue(true)); 
    67         adminMock.expects(once()).method("userProxy").will(returnValue(user)); 
    68         adminMock.expects(once()).method("groupProxy").will(returnValue(group)); 
    69         adminMock.expects(once()).method("getMemberOfGroupIds").will( 
     116        sf.mockAdmin.expects(atLeastOnce()).method("userProxy").will( 
     117                returnValue(user)); 
     118        sf.mockAdmin.expects(atLeastOnce()).method("groupProxy").will( 
     119                returnValue(group)); 
     120        sf.mockAdmin.expects(atLeastOnce()).method("getMemberOfGroupIds").will( 
    70121                returnValue(m_ids)); 
    71         adminMock.expects(once()).method("getLeaderOfGroupIds").will( 
     122        sf.mockAdmin.expects(atLeastOnce()).method("getLeaderOfGroupIds").will( 
    72123                returnValue(l_ids)); 
    73         adminMock.expects(once()).method("getUserRoles").will( 
     124        sf.mockAdmin.expects(atLeastOnce()).method("getUserRoles").will( 
    74125                returnValue(userRoles)); 
    75         adminMock.expects(once()).method("checkPassword").will( 
     126        sf.mockAdmin.expects(once()).method("checkPassword").will( 
    76127                returnValue(true)); 
    77         queryMock.expects(once()).method("findAllByQuery") 
     128        sf.mockQuery.expects(once()).method("findAllByQuery") 
    78129                .will( 
    79130                        returnValue(Collections 
    80131                                .singletonList(new ExperimenterGroup()))); 
    81         updateMock.expects(once()).method("saveObject"); 
     132        sf.mockUpdate.expects(atLeastOnce()).method("saveAndReturnObject") 
     133                .will(returnValue(session)); 
     134 
     135    } 
     136 
     137    void prepareSessionUpdate() { 
     138        prepareSessionCreation(); 
     139        // exMock.expects(atLeastOnce()).method("execute").will(new 
     140        // DoWorkStub()); 
     141        // 
     142        // sf.mockAdmin.expects(once()).method("userProxy") 
     143        // .will(returnValue(user)); 
     144        // sf.mockAdmin.expects(atLeastOnce()).method("groupProxy").will( 
     145        // returnValue(group)); 
     146        // sf.mockUpdate.expects(once()).method("saveAndReturnObject").will( 
     147        // new SetIdStub(1L)); 
     148        // 
    82149    } 
    83150 
    … …  
    91158 
    92159        prepareSessionCreation(); 
    93         session = mgr.create(principal, credentials); 
     160        assert session == mgr.create(principal, credentials); 
    94161        assertNotNull(session); 
    95162        assertNotNull(session.getUuid()); 
    … …  
    141208        testCreateNewSession(); 
    142209        session.setDefaultEventType("somethingnew"); 
    143         updateMock.expects(once()).method("saveAndReturnObject").will( 
     210        sf.mockUpdate.expects(once()).method("saveAndReturnObject").will( 
    144211                returnValue(rv)); 
     212 
     213        prepareSessionUpdate(); 
    145214        Session test = mgr.update(session); 
    146         assertTrue(test == rv); // insists that a copy is performed 
     215        assertFalse(test == rv); // insists that a copy is performed 
    147216    } 
    148217 
    … …  
    204273 
    205274        }; 
    206         updateMock.expects(once()).method("saveObject").with(closedSession); 
     275        sf.mockUpdate.expects(once()).method("saveObject").with(closedSession); 
    207276        mgr.close(session.getUuid()); 
    208277        assertNull(mgr.find(session.getUuid())); 
    … …  
    224293 
    225294        }; 
    226         updateMock.expects(once()).method("saveObject").with(closedSession); 
     295        sf.mockUpdate.expects(once()).method("saveObject").with(closedSession); 
    227296        mgr.close(session.getUuid()); 
    228297        assertNull(mgr.find(session.getUuid())); 
    … …  
    260329 
    261330    void prepareForCreateSession() { 
    262         adminMock.expects(once()).method("userProxy").will(returnValue(user)); 
    263         adminMock.expects(once()).method("groupProxy").will(returnValue(group)); 
    264         adminMock.expects(once()).method("getMemberOfGroupIds").will( 
     331        sf.mockAdmin.expects(once()).method("userProxy") 
     332                .will(returnValue(user)); 
     333        sf.mockAdmin.expects(once()).method("groupProxy").will( 
     334                returnValue(group)); 
     335        sf.mockAdmin.expects(atLeastOnce()).method("getMemberOfGroupIds").will( 
    265336                returnValue(m_ids)); 
    266         adminMock.expects(once()).method("getLeaderOfGroupIds").will( 
     337        sf.mockAdmin.expects(atLeastOnce()).method("getLeaderOfGroupIds").will( 
    267338                returnValue(l_ids)); 
    268         adminMock.expects(once()).method("getUserRoles").will( 
     339        sf.mockAdmin.expects(once()).method("getUserRoles").will( 
    269340                returnValue(userRoles)); 
    270         adminMock.expects(once()).method("checkPassword").will( 
     341        sf.mockAdmin.expects(once()).method("checkPassword").will( 
    271342                returnValue(true)); 
    272343    } 
    273344 
    274     @Test(expectedExceptions = ApiUsageException.class) 
     345    @Test(expectedExceptions = AuthenticationException.class) 
    275346    public void testCreateSessionFailsAUEOnNullPrincipal() throws Exception { 
    276         adminMock.expects(once()).method("checkPassword").will( 
     347        sf.mockAdmin.expects(once()).method("checkPassword").will( 
    277348                returnValue(false)); 
    278349        mgr.create(null, "password"); 
    279350    } 
    280351 
    281     @Test(expectedExceptions = ApiUsageException.class) 
     352    @Test(expectedExceptions = AuthenticationException.class) 
    282353    public void testCreateSessionFailsAUEOnNullOmeName() throws Exception { 
    283         adminMock.expects(once()).method("checkPassword").will( 
     354        sf.mockAdmin.expects(once()).method("checkPassword").will( 
    284355                returnValue(false)); 
    285356        mgr.create(new Principal(null, null, null), "password"); 
    286357    } 
    287358 
    288     @Test(expectedExceptions = SecurityViolation.class) 
     359    @Test(expectedExceptions = AuthenticationException.class) 
    289360    public void testCreateSessionFailsSV() throws Exception { 
    290         adminMock.expects(once()).method("checkPassword").will( 
     361        sf.mockAdmin.expects(once()).method("checkPassword").will( 
    291362                returnValue(false)); 
    292363        mgr.create(principal, "password"); 
    … …  
    296367    public void testChecksForDefaultGroupsOnCreation() throws Exception { 
    297368        prepareForCreateSession(); 
    298         queryMock.expects(once()).method("findAllByQuery").will( 
     369        sf.mockAdmin.expects(once()).method("getDefaultGroup").will( 
     370                returnValue(group)); 
     371        sf.mockQuery.expects(once()).method("findAllByQuery").will( 
    299372                returnValue(Collections.EMPTY_LIST)); 
    300373        mgr.create(new Principal("user", "user", "User"), "user"); 
    … …  
    321394        fail("NYI"); 
    322395    } 
     396 
     397    @Test 
     398    public void testReferenceCounting() throws Exception { 
     399        testCreateNewSession(); 
     400        String uuid = session.getUuid(); 
     401        SessionContext ctx = cache.getSessionContext(uuid); 
     402 
     403        assertEquals(1, ctx.refCount()); 
     404        assertNull(ctx.getSession().getClosed()); 
     405 
     406        mgr.create(new Principal(uuid)); 
     407        assertEquals(2, ctx.refCount()); 
     408        assertNull(ctx.getSession().getClosed()); 
     409 
     410        mgr.close(uuid); 
     411        assertEquals(1, ctx.refCount()); 
     412        assertNull(ctx.getSession().getClosed()); 
     413 
     414        prepareSessionUpdate(); 
     415        mgr.close(uuid); 
     416        assertEquals(0, ctx.refCount()); 
     417        assertNotNull(ctx.getSession().getClosed()); 
     418    } 
    323419} 

Download in other formats:

  • Unified Diff
  • Zip Archive

Trac Powered

Powered by Trac 0.11
By Edgewall Software.

Visit the Trac open source project at
http://trac.edgewall.org/