aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/di/DirConfigSource.java
blob: afbc51f0da4ed4798fe49ff8598804bd2ff19143 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.di;

import com.yahoo.config.subscription.ConfigSource;
import com.yahoo.config.subscription.DirSource;
import com.yahoo.config.subscription.FileSource;
import org.junit.jupiter.api.Assertions;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author Tony Vaagenes
 * @author gjoranv
 * @author ollivir
 */
class DirConfigSource {

    private final Set<String> checked = new HashSet<>();
    private final File tempFolder;
    private boolean doubleChecked;

    DirConfigSource(File tmpDir) {
        this.tempFolder = tmpDir;
    }

    void writeConfig(String name, String contents) {
        try {
            Files.writeString(tempFolder.toPath().resolve(name + ".cfg"), contents);
        }
        catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    String configId() {
        return "dir:" + tempFolder.getPath();
    }

    synchronized void clearCheckedConfigs() {
        checked.clear();
        doubleChecked = false;
    }

    synchronized void awaitConfigChecked(long millis) throws InterruptedException {
         long remaining, doom = System.currentTimeMillis() + millis;
         while ( ! doubleChecked && (remaining = doom - System.currentTimeMillis()) > 0) wait(remaining);
        assertTrue(doubleChecked, "some config should be checked more than once during " + millis + " millis; checked ones: " + checked);
    }

    ConfigSource configSource() {
        return new DirSource(tempFolder) {
            @Override public FileSource getFile(String name) {
                return new FileSource(new File(tempFolder, name)) {
                    @Override public long getLastModified() {
                        synchronized (DirConfigSource.this) {
                            if ( ! checked.add(name)) {
                                doubleChecked = true;
                                DirConfigSource.this.notifyAll();
                            }
                            return super.getLastModified();
                        }
                    }
                };
            }
        };
    }

}