1 package com.atlassian.vcache.internal.core.service;
2
3 import com.atlassian.vcache.RequestCache;
4 import com.atlassian.vcache.internal.RequestContext;
5 import com.atlassian.vcache.internal.core.WorkContextRequestContextSupplier;
6 import com.atlassian.workcontext.api.EstablishWorkContext;
7 import com.atlassian.workcontext.api.WorkContextFuture;
8 import org.junit.Rule;
9 import org.junit.Test;
10 import org.junit.rules.ExpectedException;
11
12 import java.time.Duration;
13 import java.util.Optional;
14
15 import static org.hamcrest.Matchers.is;
16 import static org.hamcrest.Matchers.not;
17 import static org.hamcrest.Matchers.notNullValue;
18 import static org.junit.Assert.assertThat;
19
20 public class WorkContextRequestContextSupplierTest {
21 @Rule
22 public ExpectedException thrown = ExpectedException.none();
23
24 @Rule
25 public EstablishWorkContext establishWorkContext = new EstablishWorkContext();
26
27 @Test
28 public void normal_flow() {
29 final WorkContextRequestContextSupplier supplier = new WorkContextRequestContextSupplier(() -> "abc");
30
31 final RequestContext context = supplier.get();
32
33 assertThat(context, notNullValue());
34 assertThat(context.partitionIdentifier(), is("abc"));
35 }
36
37 @Test
38 public void add_to_cache_across_threads() {
39 final RequestCache<String, String> cache =
40 new DefaultRequestCache<>("cache", new WorkContextRequestContextSupplier(() -> "abc"), Duration.ofSeconds(10));
41 cache.put("1", "2");
42 final String threadName = Thread.currentThread().getName();
43
44
45 WorkContextFuture.runAsync( () -> {
46 assertThat(Thread.currentThread().getName(), not(threadName));
47 assertThat(cache.get("1"), is(Optional.of("2")));
48 cache.put("2", "2");
49 }).join();
50
51 assertThat(cache.get("2"), is(Optional.of("2")));
52 }
53 }