Skip to content

Commit

Permalink
TAP5-2799: Session.LockMode introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
benweidig committed Dec 9, 2024
1 parent d42450e commit 36b94e0
Show file tree
Hide file tree
Showing 4 changed files with 347 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2006, 2007, 2008 The Apache Software Foundation
// Copyright 2006, 2007, 2008, 2024 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,11 +27,19 @@ public class PageTesterSession implements Session
{
private final Map<String, Object> attributes = CollectionFactory.newMap();

@Override
public List<String> getAttributeNames()
{
return InternalUtils.sortedKeys(attributes);
}

@Override
public List<String> getAttributeNames(Session.LockMode lockMode)
{
return getAttributeNames();
}

@Override
public List<String> getAttributeNames(String prefix)
{
List<String> result = newList();
Expand All @@ -42,11 +50,25 @@ public List<String> getAttributeNames(String prefix)
return result;
}

@Override
public List<String> getAttributeNames(String prefix, LockMode lockMode)
{
return getAttributeNames(prefix);
}

@Override
public Object getAttribute(String name)
{
return attributes.get(name);
}

@Override
public Object getAttribute(String name, LockMode lockMode)
{
return getAttribute(name);
}

@Override
public void setAttribute(String name, Object value)
{
if (value == null)
Expand All @@ -59,33 +81,50 @@ public void setAttribute(String name, Object value)
}
}

@Override
public boolean containsAttribute(String name)
{
return attributes.containsKey(name);
}

@Override
public boolean containsAttribute(String name, Session.LockMode lockMode)
{
return containsAttribute(name);
}

private void nyi(String name)
{
throw new IllegalStateException(String.format("%s.%s() is not yet implemented.", getClass()
.getName(), name));
}

@Override
public int getMaxInactiveInterval()
{
nyi("getMaxInativeInterval");

return 0;
}

@Override
public void invalidate()
{
nyi("invalidate");
}

@Override
public boolean isInvalidated()
{
return false;
}

@Override
public void restoreDirtyObjects()
{
}

@Override
public void setMaxInactiveInterval(int seconds)
{
nyi("setMaxInactiveInterval");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

package org.apache.tapestry5.internal.services;

import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import org.apache.tapestry5.http.internal.services.ClusteredSessionImpl;
import org.apache.tapestry5.http.internal.services.SessionImpl;
import org.apache.tapestry5.http.internal.services.SessionLock;
Expand All @@ -24,10 +29,6 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.concurrent.locks.ReentrantLock;

public class SessionImplTest extends InternalBaseTestCase
{
Expand Down Expand Up @@ -55,6 +56,25 @@ public void get_attribute_names()
verify();
}

@Test
public void get_attribute_names_lock_mode()
{
Enumeration e = Collections.enumeration(Arrays.asList("fred", "barney"));
HttpSession hs = mockHttpSession();
SessionLock lock = mockLock();

lock.acquireWriteLock();
expect(hs.getAttributeNames()).andReturn(e);

replay();

Session session = new SessionImpl(null, hs, lock);

assertEquals(session.getAttributeNames(Session.LockMode.WRITE), Arrays.asList("barney", "fred"));

verify();
}

@Test
public void get_attribute_names_by_prefix()
{
Expand All @@ -75,6 +95,71 @@ public void get_attribute_names_by_prefix()
verify();
}

@Test
public void contains_attribute()
{
List<String> keys = Arrays.asList("fred", "barney");
HttpSession hs = mockHttpSession();
SessionLock lock = mockLock();

// We need one per assert, and Enumerations are exhausted on use
lock.acquireReadLock();
expect(hs.getAttributeNames()).andReturn(Collections.enumeration(keys));
lock.acquireReadLock();
expect(hs.getAttributeNames()).andReturn(Collections.enumeration(keys));
lock.acquireReadLock();
expect(hs.getAttributeNames()).andReturn(Collections.enumeration(keys));
replay();

Session session = new SessionImpl(null, hs, lock);

assertTrue(session.containsAttribute("barney"));
assertTrue(session.containsAttribute("fred"));
assertFalse(session.containsAttribute("wilma"));

verify();
}

@Test
public void get_attribute_write_lock()
{
Enumeration e = Collections.enumeration(Arrays.asList("fred", "barney"));
HttpSession hs = mockHttpSession();
SessionLock lock = mockLock();

lock.acquireReadLock();
lock.acquireWriteLock();
expect(hs.getAttributeNames()).andReturn(e);
expect(hs.getAttribute("fred")).andReturn("1");

replay();

Session session = new SessionImpl(null, hs, lock);

assertEquals(session.getAttribute("fred"), "1");

verify();
}

@Test
public void get_attribute_no_lock_update()
{
Enumeration e = Collections.enumeration(Arrays.asList("fred", "barney"));
HttpSession hs = mockHttpSession();
SessionLock lock = mockLock();

lock.acquireReadLock();
expect(hs.getAttributeNames()).andReturn(e);

replay();

Session session = new SessionImpl(null, hs, lock);

assertEquals(session.getAttribute("wilma"), null);

verify();
}

@Test
public void invalidate()
{
Expand Down
Loading

0 comments on commit 36b94e0

Please sign in to comment.