summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionRepoTest.java
blob: 5753b2959f77ac2c242ffb517a0eda736c4c6245 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.session;

import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.path.Path;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.config.server.*;
import com.yahoo.config.provision.TenantName;
import com.yahoo.vespa.config.server.application.MemoryTenantApplications;
import com.yahoo.vespa.config.server.deploy.TenantFileSystemDirs;
import com.yahoo.io.IOUtils;
import com.yahoo.vespa.config.server.host.HostRegistry;
import com.yahoo.vespa.config.server.http.SessionHandlerTest;
import com.yahoo.vespa.config.server.zookeeper.SessionCounter;

import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.time.Duration;
import java.time.Instant;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

/**
 * @author lulf
 * @since 5.1
 */
public class LocalSessionRepoTest extends TestWithCurator {

    private File testApp = new File("src/test/apps/app");
    private LocalSessionRepo repo;
    private ManualClock clock;
    private static final TenantName tenantName = TenantName.defaultName();

    @Before
    public void setupSessions() throws Exception {
        setupSessions(tenantName, true);
    }

    private void setupSessions(TenantName tenantName, boolean createInitialSessions) throws Exception {
        GlobalComponentRegistry globalComponentRegistry = new TestComponentRegistry.Builder().curator(curator).build();
        TenantFileSystemDirs tenantFileSystemDirs = TenantFileSystemDirs.createTestDirs(tenantName);
        if (createInitialSessions) {
            IOUtils.copyDirectory(testApp, new File(tenantFileSystemDirs.sessionsPath(), "1"));
            IOUtils.copyDirectory(testApp, new File(tenantFileSystemDirs.sessionsPath(), "2"));
            IOUtils.copyDirectory(testApp, new File(tenantFileSystemDirs.sessionsPath(), "3"));
        }
        clock = new ManualClock(Instant.ofEpochSecond(1));
        LocalSessionLoader loader = new SessionFactoryImpl(globalComponentRegistry,
                new SessionCounter(globalComponentRegistry.getCurator(),
                        Path.fromString("counter"),
                        Path.fromString("sessions")),
                Path.createRoot(),
                new MemoryTenantApplications(),
                tenantFileSystemDirs, new HostRegistry<>(),
                tenantName);
        repo = new LocalSessionRepo(tenantFileSystemDirs, loader, clock, 5);
    }

    @Test
    public void require_that_sessions_can_be_loaded_from_disk() {
        assertNotNull(repo.getSession(1l));
        assertNotNull(repo.getSession(2l));
        assertNotNull(repo.getSession(3l));
        assertNull(repo.getSession(4l));
    }

    @Test
    public void require_that_old_sessions_are_purged() {
        clock.advance(Duration.ofSeconds(1));
        assertNotNull(repo.getSession(1l));
        assertNotNull(repo.getSession(2l));
        assertNotNull(repo.getSession(3l));
        clock.advance(Duration.ofSeconds(1));
        assertNotNull(repo.getSession(1l));
        assertNotNull(repo.getSession(2l));
        assertNotNull(repo.getSession(3l));
        clock.advance(Duration.ofSeconds(1));
        addSession(4l, 6);
        assertNotNull(repo.getSession(1l));
        assertNotNull(repo.getSession(2l));
        assertNotNull(repo.getSession(3l));
        assertNotNull(repo.getSession(4l));
        clock.advance(Duration.ofSeconds(1));
        addSession(5l, 10);
        assertNull(repo.getSession(1l));
        assertNull(repo.getSession(2l));
        assertNull(repo.getSession(3l));
    }

    @Test
    public void require_that_all_sessions_are_deleted() {
        repo.deleteAllSessions();
        assertNull(repo.getSession(1l));
        assertNull(repo.getSession(2l));
        assertNull(repo.getSession(3l));
    }

    private void addSession(long sessionId, long createTime) {
        repo.addSession(new SessionHandlerTest.MockSession(sessionId, FilesApplicationPackage.fromFile(testApp), createTime));
    }

    @Test
    public void require_that_sessions_belong_to_a_tenant() {
        // tenant is "default"
        assertNotNull(repo.getSession(1l));
        assertNotNull(repo.getSession(2l));
        assertNotNull(repo.getSession(3l));
        assertNull(repo.getSession(4l));

        // tenant is "newTenant"
        try {
            setupSessions(TenantName.from("newTenant"), false);
        } catch (Exception e) {
            fail();
        }
        assertNull(repo.getSession(1l));

        repo.addSession(new SessionHandlerTest.MockSession(1l, FilesApplicationPackage.fromFile(testApp)));
        repo.addSession(new SessionHandlerTest.MockSession(2l, FilesApplicationPackage.fromFile(testApp)));
        assertNotNull(repo.getSession(1l));
        assertNotNull(repo.getSession(2l));
        assertNull(repo.getSession(3l));
    }
}