diff options
author | Harald Musum <musum@verizonmedia.com> | 2020-06-15 08:18:37 +0200 |
---|---|---|
committer | Harald Musum <musum@verizonmedia.com> | 2020-06-15 08:18:37 +0200 |
commit | e08d52807dfa6889ebf7c5a01225c955133e1f55 (patch) | |
tree | 22c44308eedbf7a20c5daa0e665c0a34eae21f31 /configserver | |
parent | 0e4a1ad6ed04f8b51e6c79b4b82b597bcdc38b0c (diff) |
Rewrite tests to use less low-level code
* Rewrite and merge some tests
* Also rename some methods to make it clear that they handle local sessions
Diffstat (limited to 'configserver')
8 files changed, 120 insertions, 122 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java index 2838bba4784..9625bdf7447 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java @@ -668,7 +668,7 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye public void deleteExpiredLocalSessions() { Map<Tenant, List<LocalSession>> sessionsPerTenant = new HashMap<>(); - tenantRepository.getAllTenants().forEach(tenant -> sessionsPerTenant.put(tenant, tenant.getSessionRepository().getSessions())); + tenantRepository.getAllTenants().forEach(tenant -> sessionsPerTenant.put(tenant, tenant.getSessionRepository().getLocalSessions())); Set<ApplicationId> applicationIds = new HashSet<>(); sessionsPerTenant.values().forEach(sessionList -> sessionList.forEach(s -> applicationIds.add(s.getApplicationId()))); @@ -757,7 +757,7 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye } private LocalSession getLocalSession(Tenant tenant, long sessionId) { - LocalSession session = tenant.getSessionRepository().getSession(sessionId); + LocalSession session = tenant.getSessionRepository().getLocalSession(sessionId); if (session == null) throw new NotFoundException("Session " + sessionId + " was not found"); return session; @@ -823,7 +823,7 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye public LocalSession getActiveLocalSession(Tenant tenant, ApplicationId applicationId) { TenantApplications applicationRepo = tenant.getApplicationRepo(); if (applicationRepo.activeApplications().contains(applicationId)) { - return tenant.getSessionRepository().getSession(applicationRepo.requireActiveSessionOf(applicationId)); + return tenant.getSessionRepository().getLocalSession(applicationRepo.requireActiveSessionOf(applicationId)); } return null; } diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/session/LocalSessionStateWatcher.java b/configserver/src/main/java/com/yahoo/vespa/config/server/session/LocalSessionStateWatcher.java index 1067a373f41..acbb1dc81ce 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/session/LocalSessionStateWatcher.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/session/LocalSessionStateWatcher.java @@ -40,9 +40,9 @@ public class LocalSessionStateWatcher { log.log(status == Session.Status.DELETE ? Level.INFO : Level.FINE, session.logPre() + "Session change: Local session " + sessionId + " changed status to " + status); - if (status.equals(Session.Status.DELETE) && sessionRepository.getSession(sessionId) != null) { + if (status.equals(Session.Status.DELETE) && sessionRepository.getLocalSession(sessionId) != null) { log.log(Level.FINE, session.logPre() + "Deleting session " + sessionId); - sessionRepository.deleteSession(session); + sessionRepository.deleteLocalSession(session); } } diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionRepository.java b/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionRepository.java index c6e33ed39ef..9f89de3035a 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionRepository.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionRepository.java @@ -126,11 +126,11 @@ public class SessionRepository { localSessionStateWatchers.put(sessionId, new LocalSessionStateWatcher(fileCache, session, this, zkWatcherExecutor)); } - public LocalSession getSession(long sessionId) { + public LocalSession getLocalSession(long sessionId) { return localSessionCache.getSession(sessionId); } - public List<LocalSession> getSessions() { + public List<LocalSession> getLocalSessions() { return localSessionCache.getSessions(); } @@ -178,13 +178,13 @@ public class SessionRepository { // Sessions with state other than ACTIVATED if (hasExpired(candidate) && !isActiveSession(candidate)) { - deleteSession(candidate); + deleteLocalSession(candidate); } else if (createTime.plus(Duration.ofDays(1)).isBefore(clock.instant())) { // Sessions with state ACTIVATE, but which are not actually active ApplicationId applicationId = candidate.getApplicationId(); Long activeSession = activeSessions.get(applicationId); if (activeSession == null || activeSession != candidate.getSessionId()) { - deleteSession(candidate); + deleteLocalSession(candidate); log.log(Level.INFO, "Deleted inactive session " + candidate.getSessionId() + " created " + createTime + " for '" + applicationId + "'"); } @@ -205,7 +205,7 @@ public class SessionRepository { return candidate.getStatus() == Session.Status.ACTIVATE; } - public void deleteSession(LocalSession session) { + public void deleteLocalSession(LocalSession session) { long sessionId = session.getSessionId(); log.log(Level.FINE, "Deleting local session " + sessionId); LocalSessionStateWatcher watcher = localSessionStateWatchers.remove(sessionId); @@ -241,7 +241,7 @@ public class SessionRepository { private void deleteAllSessions() { List<LocalSession> sessions = new ArrayList<>(localSessionCache.getSessions()); for (LocalSession session : sessions) { - deleteSession(session); + deleteLocalSession(session); } } @@ -316,7 +316,7 @@ public class SessionRepository { * * @param sessionId session id for the new session */ - private void sessionAdded(long sessionId) { + public void sessionAdded(long sessionId) { log.log(Level.FINE, () -> "Adding session to SessionRepository: " + sessionId); RemoteSession session = createRemoteSession(sessionId); Path sessionPath = sessionsPath.append(String.valueOf(sessionId)); @@ -589,7 +589,7 @@ public class SessionRepository { @Override public String toString() { - return getSessions().toString(); + return getLocalSessions().toString(); } private static class FileTransaction extends AbstractTransaction { diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java index 6039b91d2ec..f0aa38a228e 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java @@ -4,6 +4,7 @@ package com.yahoo.vespa.config.server; import com.yahoo.cloud.config.ConfigserverConfig; import com.yahoo.component.Version; import com.yahoo.config.ConfigInstance; +import com.yahoo.config.FileReference; import com.yahoo.config.SimpletypesConfig; import com.yahoo.config.application.api.ApplicationMetaData; import com.yahoo.config.model.NullConfigModelRegistry; @@ -116,10 +117,6 @@ public class ApplicationRepositoryTest { @Rule public ExpectedException exceptionRule = ExpectedException.none(); - @Rule - public TemporaryFolder tempFolder = new TemporaryFolder(); - - @Before public void setup() throws IOException { setup(new InMemoryFlagSource()); @@ -132,8 +129,9 @@ public class ApplicationRepositoryTest { .curator(curator) .configServerConfig(new ConfigserverConfig.Builder() .payloadCompressionType(ConfigserverConfig.PayloadCompressionType.Enum.UNCOMPRESSED) - .configServerDBDir(tempFolder.newFolder().getAbsolutePath()) - .configDefinitionsDir(tempFolder.newFolder().getAbsolutePath()) + .configServerDBDir(temporaryFolder.newFolder().getAbsolutePath()) + .configDefinitionsDir(temporaryFolder.newFolder().getAbsolutePath()) + .fileReferencesDir(temporaryFolder.newFolder().getAbsolutePath()) .build()) .flagSource(flagSource) .build(); @@ -159,7 +157,7 @@ public class ApplicationRepositoryTest { TenantName tenantName = applicationId().tenant(); Tenant tenant = tenantRepository.getTenant(tenantName); - LocalSession session = tenant.getSessionRepository().getSession(tenant.getApplicationRepo() + LocalSession session = tenant.getSessionRepository().getLocalSession(tenant.getApplicationRepo() .requireActiveSessionOf(applicationId())); session.getAllocatedHosts(); } @@ -191,7 +189,7 @@ public class ApplicationRepositoryTest { TenantName tenantName = applicationId().tenant(); Tenant tenant = tenantRepository.getTenant(tenantName); - LocalSession session = tenant.getSessionRepository().getSession( + LocalSession session = tenant.getSessionRepository().getLocalSession( tenant.getApplicationRepo().requireActiveSessionOf(applicationId())); assertEquals(firstSessionId, session.getMetaData().getPreviousActiveGeneration()); } @@ -309,10 +307,10 @@ public class ApplicationRepositoryTest { { PrepareResult result = deployApp(testApp); long sessionId = result.sessionId(); - LocalSession applicationData = tenant.getSessionRepository().getSession(sessionId); + LocalSession applicationData = tenant.getSessionRepository().getLocalSession(sessionId); assertNotNull(applicationData); assertNotNull(applicationData.getApplicationId()); - assertNotNull(tenant.getSessionRepo().getSession(sessionId)); + assertNotNull(tenant.getSessionRepo().getLocalSession(sessionId)); assertNotNull(applicationRepository.getActiveSession(applicationId())); String sessionNode = TenantRepository.getSessionsPath(tenantName).append(String.valueOf(sessionId)).getAbsolute(); assertTrue(configCurator.exists(sessionNode)); @@ -323,8 +321,8 @@ public class ApplicationRepositoryTest { // Delete app and verify that it has been deleted from repos and provisioner assertTrue(applicationRepository.delete(applicationId())); assertNull(applicationRepository.getActiveSession(applicationId())); - assertNull(tenant.getSessionRepository().getSession(sessionId)); - assertNull(tenant.getSessionRepo().getSession(sessionId)); + assertNull(tenant.getSessionRepository().getLocalSession(sessionId)); + assertNull(tenant.getSessionRepo().getLocalSession(sessionId)); assertTrue(provisioner.removed); assertEquals(tenant.getName(), provisioner.lastApplicationId.tenant()); assertEquals(applicationId(), provisioner.lastApplicationId); @@ -366,7 +364,7 @@ public class ApplicationRepositoryTest { // A new delete should cleanup and be successful RemoteSession activeSession = applicationRepository.getActiveSession(applicationId()); assertNull(activeSession); - assertNull(tenant.getSessionRepo().getSession(prepareResult.sessionId())); + assertNull(tenant.getSessionRepo().getLocalSession(prepareResult.sessionId())); assertTrue(applicationRepository.delete(applicationId())); } @@ -400,13 +398,13 @@ public class ApplicationRepositoryTest { // No change to active session id assertEquals(activeSessionId, tester.tenant().getApplicationRepo().requireActiveSessionOf(tester.applicationId())); SessionRepository sessionRepository = tester.tenant().getSessionRepository(); - assertEquals(3, sessionRepository.getSessions().size()); + assertEquals(3, sessionRepository.getLocalSessions().size()); clock.advance(Duration.ofHours(1)); // longer than session lifetime // All sessions except 3 should be removed after the call to deleteExpiredLocalSessions tester.applicationRepository().deleteExpiredLocalSessions(); - Collection<LocalSession> sessions = sessionRepository.getSessions(); + Collection<LocalSession> sessions = sessionRepository.getLocalSessions(); assertEquals(1, sessions.size()); ArrayList<LocalSession> localSessions = new ArrayList<>(sessions); LocalSession localSession = localSessions.get(0); @@ -420,9 +418,9 @@ public class ApplicationRepositoryTest { assertTrue(deployment4.isPresent()); deployment4.get().prepare(); // session 5 (not activated) - assertEquals(2, sessionRepository.getSessions().size()); - sessionRepository.deleteSession(localSession); - assertEquals(1, sessionRepository.getSessions().size()); + assertEquals(2, sessionRepository.getLocalSessions().size()); + sessionRepository.deleteLocalSession(localSession); + assertEquals(1, sessionRepository.getLocalSessions().size()); // Check that trying to expire when there are no active sessions works tester.applicationRepository().deleteExpiredLocalSessions(); @@ -477,7 +475,7 @@ public class ApplicationRepositoryTest { TenantName tenantName = applicationId().tenant(); Tenant tenant = tenantRepository.getTenant(tenantName); - LocalSession session = tenant.getSessionRepository().getSession(tenant.getApplicationRepo().requireActiveSessionOf(applicationId())); + LocalSession session = tenant.getSessionRepository().getLocalSession(tenant.getApplicationRepo().requireActiveSessionOf(applicationId())); List<NetworkPorts.Allocation> list = new ArrayList<>(); list.add(new NetworkPorts.Allocation(8080, "container", "container/container.0", "http")); diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/DeployTester.java b/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/DeployTester.java index bc9f45698e9..7a8bad3d199 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/DeployTester.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/DeployTester.java @@ -249,7 +249,7 @@ public class DeployTester { public AllocatedHosts getAllocatedHostsOf(ApplicationId applicationId) { Tenant tenant = tenant(); - LocalSession session = tenant.getSessionRepository().getSession(tenant.getApplicationRepo() + LocalSession session = tenant.getSessionRepository().getLocalSession(tenant.getApplicationRepo() .requireActiveSessionOf(applicationId)); return session.getAllocatedHosts(); } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/RedeployTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/RedeployTest.java index 156200a061c..c07c7316930 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/RedeployTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/RedeployTest.java @@ -33,11 +33,11 @@ public class RedeployTest { assertTrue(deployment.isPresent()); long activeSessionIdBefore = tester.applicationRepository().getActiveSession(tester.applicationId()).getSessionId(); - assertEquals(tester.applicationId(), tester.tenant().getSessionRepository().getSession(activeSessionIdBefore).getApplicationId()); + assertEquals(tester.applicationId(), tester.tenant().getSessionRepository().getLocalSession(activeSessionIdBefore).getApplicationId()); deployment.get().activate(); long activeSessionIdAfter = tester.applicationRepository().getActiveSession(tester.applicationId()).getSessionId(); assertEquals(activeSessionIdAfter, activeSessionIdBefore + 1); - assertEquals(tester.applicationId(), tester.tenant().getSessionRepository().getSession(activeSessionIdAfter).getApplicationId()); + assertEquals(tester.applicationId(), tester.tenant().getSessionRepository().getLocalSession(activeSessionIdAfter).getApplicationId()); } /** No deployment is done because there is no local active session. */ diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java index ec141a61ee6..0a5221b2c97 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java @@ -286,13 +286,13 @@ public class ApplicationHandlerTest { private void deleteAndAssertOKResponseMocked(ApplicationId applicationId, boolean fullAppIdInUrl) throws IOException { long sessionId = tenantRepository.getTenant(applicationId.tenant()).getApplicationRepo().requireActiveSessionOf(applicationId); deleteAndAssertResponse(applicationId, Zone.defaultZone(), Response.Status.OK, null, fullAppIdInUrl); - assertNull(tenantRepository.getTenant(applicationId.tenant()).getSessionRepository().getSession(sessionId)); + assertNull(tenantRepository.getTenant(applicationId.tenant()).getSessionRepository().getLocalSession(sessionId)); } private void deleteAndAssertOKResponse(Tenant tenant, ApplicationId applicationId) throws IOException { long sessionId = tenant.getApplicationRepo().requireActiveSessionOf(applicationId); deleteAndAssertResponse(applicationId, Zone.defaultZone(), Response.Status.OK, null, true); - assertNull(tenant.getSessionRepository().getSession(sessionId)); + assertNull(tenant.getSessionRepository().getLocalSession(sessionId)); } private void deleteAndAssertResponse(ApplicationId applicationId, Zone zone, int expectedStatus, HttpErrorResponse.errorCodes errorCode, boolean fullAppIdInUrl) throws IOException { diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/session/SessionRepositoryTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/session/SessionRepositoryTest.java index 1528c82188e..b9e872261c5 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/session/SessionRepositoryTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/session/SessionRepositoryTest.java @@ -2,28 +2,27 @@ package com.yahoo.vespa.config.server.session; import com.yahoo.cloud.config.ConfigserverConfig; -import com.yahoo.config.model.application.provider.FilesApplicationPackage; +import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.TenantName; -import com.yahoo.io.IOUtils; import com.yahoo.text.Utf8; +import com.yahoo.vespa.config.server.ApplicationRepository; import com.yahoo.vespa.config.server.GlobalComponentRegistry; import com.yahoo.vespa.config.server.TestComponentRegistry; -import com.yahoo.vespa.config.server.application.TenantApplications; +import com.yahoo.vespa.config.server.application.OrchestratorMock; import com.yahoo.vespa.config.server.http.SessionHandlerTest; import com.yahoo.vespa.config.server.tenant.Tenant; import com.yahoo.vespa.config.server.tenant.TenantRepository; import com.yahoo.vespa.config.server.zookeeper.ConfigCurator; import com.yahoo.vespa.curator.Curator; import com.yahoo.vespa.curator.mock.MockCurator; +import com.yahoo.vespa.flags.FlagSource; import com.yahoo.vespa.flags.InMemoryFlagSource; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import java.io.File; -import java.nio.file.Path; -import java.nio.file.Paths; +import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.util.function.LongPredicate; @@ -32,131 +31,106 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; /** * @author Ulf Lilleengen */ public class SessionRepositoryTest { - private File testApp = new File("src/test/apps/app"); + private static final TenantName tenantName = TenantName.defaultName(); + private static final ApplicationId applicationId = ApplicationId.from(tenantName.value(), "testApp", "default"); + private static final File testApp = new File("src/test/apps/app"); + private MockCurator curator; - private SessionRepository sessionRepository; private TenantRepository tenantRepository; - private static final TenantName tenantName = TenantName.defaultName(); + private ApplicationRepository applicationRepository; @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); - @Before - public void setupSessions() throws Exception { - setupSessions(tenantName, true); + public void setup() throws Exception { + setup(new InMemoryFlagSource()); } - private void setupSessions(TenantName tenantName, boolean createInitialSessions) throws Exception { - File configserverDbDir = temporaryFolder.newFolder().getAbsoluteFile(); - if (createInitialSessions) { - Path sessionsPath = Paths.get(configserverDbDir.getAbsolutePath(), "tenants", tenantName.value(), "sessions"); - IOUtils.copyDirectory(testApp, sessionsPath.resolve("1").toFile()); - IOUtils.copyDirectory(testApp, sessionsPath.resolve("2").toFile()); - IOUtils.copyDirectory(testApp, sessionsPath.resolve("3").toFile()); - } + private void setup(FlagSource flagSource) throws Exception { curator = new MockCurator(); - curator.create(TenantRepository.getTenantPath(tenantName).append("/applications")); - curator.create(TenantRepository.getSessionsPath(tenantName)); + File configserverDbDir = temporaryFolder.newFolder().getAbsoluteFile(); GlobalComponentRegistry globalComponentRegistry = new TestComponentRegistry.Builder() .curator(curator) .configServerConfig(new ConfigserverConfig.Builder() .configServerDBDir(configserverDbDir.getAbsolutePath()) .configDefinitionsDir(temporaryFolder.newFolder().getAbsolutePath()) + .fileReferencesDir(temporaryFolder.newFolder().getAbsolutePath()) .sessionLifetime(5) .build()) + .flagSource(flagSource) .build(); tenantRepository = new TenantRepository(globalComponentRegistry, false); - TenantApplications applicationRepo = TenantApplications.create(globalComponentRegistry, tenantName); - sessionRepository = new SessionRepository(tenantName, globalComponentRegistry, - applicationRepo, applicationRepo, new InMemoryFlagSource(), - globalComponentRegistry.getSessionPreparer()); + tenantRepository.addTenant(SessionRepositoryTest.tenantName); + applicationRepository = new ApplicationRepository(tenantRepository, + new SessionHandlerTest.MockProvisioner(), + new OrchestratorMock(), + Clock.systemUTC()); } @Test - public void require_that_sessions_can_be_loaded_from_disk() { - assertNotNull(sessionRepository.getSession(1L)); - assertNotNull(sessionRepository.getSession(2L)); - assertNotNull(sessionRepository.getSession(3L)); - assertNull(sessionRepository.getSession(4L)); - } + public void require_that_local_sessions_are_created_and_deleted() throws Exception { + setup(); + long firstSessionId = deploy(); + long secondSessionId = deploy(); + SessionRepository sessionRepository = tenantRepository.getTenant(tenantName).getSessionRepository(); + assertNotNull(sessionRepository.getLocalSession(firstSessionId)); + assertNotNull(sessionRepository.getLocalSession(secondSessionId)); + assertNull(sessionRepository.getLocalSession(secondSessionId + 1)); - @Test - public void require_that_all_sessions_are_deleted() { sessionRepository.close(); - assertNull(sessionRepository.getSession(1L)); - assertNull(sessionRepository.getSession(2L)); - assertNull(sessionRepository.getSession(3L)); + // All created sessions are deleted + assertNull(sessionRepository.getLocalSession(firstSessionId)); + assertNull(sessionRepository.getLocalSession(secondSessionId)); } @Test - public void require_that_sessions_belong_to_a_tenant() { + public void require_that_local_sessions_belong_to_a_tenant() throws Exception { + setup(); // tenant is "default" - assertNotNull(sessionRepository.getSession(1L)); - assertNotNull(sessionRepository.getSession(2L)); - assertNotNull(sessionRepository.getSession(3L)); - assertNull(sessionRepository.getSession(4L)); - - // tenant is "newTenant" - try { - setupSessions(TenantName.from("newTenant"), false); - } catch (Exception e) { - fail(); - } - assertNull(sessionRepository.getSession(1L)); - sessionRepository.addSession(new SessionHandlerTest.MockLocalSession(1L, FilesApplicationPackage.fromFile(testApp))); - sessionRepository.addSession(new SessionHandlerTest.MockLocalSession(2L, FilesApplicationPackage.fromFile(testApp))); - assertNotNull(sessionRepository.getSession(1L)); - assertNotNull(sessionRepository.getSession(2L)); - assertNull(sessionRepository.getSession(3L)); - } + long firstSessionId = deploy(); + long secondSessionId = deploy(); + SessionRepository sessionRepository = tenantRepository.getTenant(tenantName).getSessionRepository(); + assertNotNull(sessionRepository.getLocalSession(firstSessionId)); + assertNotNull(sessionRepository.getLocalSession(secondSessionId)); + assertNull(sessionRepository.getLocalSession(secondSessionId + 1)); - private void createSession(long sessionId, boolean wait) { - createSession(sessionId, wait, tenantName); - } - - private void createSession(long sessionId, boolean wait, TenantName tenantName) { - com.yahoo.path.Path sessionsPath = TenantRepository.getSessionsPath(tenantName); - SessionZooKeeperClient zkc = new SessionZooKeeperClient(curator, sessionsPath.append(String.valueOf(sessionId))); - zkc.createNewSession(Instant.now()); - if (wait) { - Curator.CompletionWaiter waiter = zkc.getUploadWaiter(); - waiter.awaitCompletion(Duration.ofSeconds(120)); - } + // tenant is "newTenant" + TenantName newTenant = TenantName.from("newTenant"); + tenantRepository.addTenant(newTenant); + long sessionId = deploy(ApplicationId.from(newTenant.value(), "testapp", "default")); + SessionRepository sessionRepository2 = tenantRepository.getTenant(newTenant).getSessionRepository(); + assertNotNull(sessionRepository2.getLocalSession(sessionId)); } @Test - public void testInitialize() { + public void testInitialize() throws Exception { + setup(); createSession(10L, false); createSession(11L, false); - assertSessionExists(10L); - assertSessionExists(11L); - } - - @Test - public void testCreateSession() { - createSession(12L, true); - assertSessionExists(12L); + assertRemoteSessionExists(10L); + assertRemoteSessionExists(11L); } @Test public void testSessionStateChange() throws Exception { + setup(); long sessionId = 3L; createSession(sessionId, true); - assertSessionStatus(sessionId, Session.Status.NEW); + assertRemoteSessionStatus(sessionId, Session.Status.NEW); assertStatusChange(sessionId, Session.Status.PREPARE); assertStatusChange(sessionId, Session.Status.ACTIVATE); com.yahoo.path.Path session = TenantRepository.getSessionsPath(tenantName).append("" + sessionId); curator.delete(session); assertSessionRemoved(sessionId); + SessionRepository sessionRepository = tenantRepository.getTenant(tenantName).getSessionRepository(); assertNull(sessionRepository.getRemoteSession(sessionId)); } @@ -165,36 +139,53 @@ public class SessionRepositoryTest { // repo even if it had bad data (by making getSessionIdForApplication() in FailingTenantApplications // throw an exception). @Test - public void testBadApplicationRepoOnActivate() { + public void testBadApplicationRepoOnActivate() throws Exception { + setup(); long sessionId = 3L; TenantName mytenant = TenantName.from("mytenant"); curator.set(TenantRepository.getApplicationsPath(mytenant).append("mytenant:appX:default"), new byte[0]); // Invalid data tenantRepository.addTenant(mytenant); Tenant tenant = tenantRepository.getTenant(mytenant); curator.create(TenantRepository.getSessionsPath(mytenant)); - sessionRepository = tenant.getSessionRepo(); + SessionRepository sessionRepository = tenant.getSessionRepo(); assertThat(sessionRepository.getRemoteSessions().size(), is(0)); createSession(sessionId, true, mytenant); assertThat(sessionRepository.getRemoteSessions().size(), is(1)); } + private void createSession(long sessionId, boolean wait) { + createSession(sessionId, wait, tenantName); + } + + private void createSession(long sessionId, boolean wait, TenantName tenantName) { + com.yahoo.path.Path sessionsPath = TenantRepository.getSessionsPath(tenantName); + SessionZooKeeperClient zkc = new SessionZooKeeperClient(curator, sessionsPath.append(String.valueOf(sessionId))); + zkc.createNewSession(Instant.now()); + if (wait) { + Curator.CompletionWaiter waiter = zkc.getUploadWaiter(); + waiter.awaitCompletion(Duration.ofSeconds(120)); + } + } + private void assertStatusChange(long sessionId, Session.Status status) throws Exception { com.yahoo.path.Path statePath = TenantRepository.getSessionsPath(tenantName).append("" + sessionId).append(ConfigCurator.SESSIONSTATE_ZK_SUBPATH); curator.create(statePath); curator.framework().setData().forPath(statePath.getAbsolute(), Utf8.toBytes(status.toString())); - assertSessionStatus(sessionId, status); + assertRemoteSessionStatus(sessionId, status); } private void assertSessionRemoved(long sessionId) { + SessionRepository sessionRepository = tenantRepository.getTenant(tenantName).getSessionRepository(); waitFor(p -> sessionRepository.getRemoteSession(sessionId) == null, sessionId); assertNull(sessionRepository.getRemoteSession(sessionId)); } - private void assertSessionExists(long sessionId) { - assertSessionStatus(sessionId, Session.Status.NEW); + private void assertRemoteSessionExists(long sessionId) { + assertRemoteSessionStatus(sessionId, Session.Status.NEW); } - private void assertSessionStatus(long sessionId, Session.Status status) { + private void assertRemoteSessionStatus(long sessionId, Session.Status status) { + SessionRepository sessionRepository = tenantRepository.getTenant(tenantName).getSessionRepository(); waitFor(p -> sessionRepository.getRemoteSession(sessionId) != null && sessionRepository.getRemoteSession(sessionId).getStatus() == status, sessionId); assertNotNull(sessionRepository.getRemoteSession(sessionId)); @@ -214,4 +205,13 @@ public class SessionRepositoryTest { } while (System.currentTimeMillis() < endTime && !ok); } + private long deploy() { + return deploy(applicationId); + } + + private long deploy(ApplicationId applicationId) { + applicationRepository.deploy(testApp, new PrepareParams.Builder().applicationId(applicationId).build()); + return applicationRepository.getActiveSession(applicationId).getSessionId(); + } + } |