1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.conscrypt;
18
19import java.io.File;
20import java.io.IOException;
21import junit.framework.TestCase;
22import libcore.javax.net.ssl.FakeSSLSession;
23
24public class FileClientSessionCacheTest extends TestCase {
25
26    public void testMaxSize() throws IOException, InterruptedException {
27        String tmpDir = System.getProperty("java.io.tmpdir");
28        if (tmpDir == null) {
29            fail("Please set 'java.io.tmpdir' system property.");
30        }
31        File cacheDir = new File(tmpDir
32                + "/" + FileClientSessionCacheTest.class.getName() + "/cache");
33        final SSLClientSessionCache cache
34                = FileClientSessionCache.usingDirectory(cacheDir);
35        Thread[] threads = new Thread[10];
36        final int iterations = FileClientSessionCache.MAX_SIZE * 10;
37        for (int i = 0; i < threads.length; i++) {
38            final int id = i;
39            threads[i] = new Thread() {
40                @Override
41                public void run() {
42                    for (int i = 0; i < iterations; i++) {
43                        cache.putSessionData(new FakeSSLSession(id + "" + i), new byte[10]);
44                    }
45                }
46            };
47        }
48        for (Thread thread : threads) {
49            thread.start();
50        }
51        for (Thread thread : threads) {
52            thread.join();
53        }
54        assertEquals(FileClientSessionCache.MAX_SIZE, cacheDir.list().length);
55    }
56}
57