private Map wrapInMap(T event, ImmutableMap data) { Long time = System.currentTimeMillis(); DateFormat format = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z"); String formatted = format.format(time); String change = Optional.ofNullable(event) .map(e -> Long.toString(Runtime.getRuntime().freeMemory() - e.getFreeMemory())).orElse("unknown"); ImmutableMap map = ImmutableMap.of("completed", time, "completed-formated", formatted, "memory-change", change); ImmutableMap.Builder builder = ImmutableMap.builder().putAll(data).putAll(map); if (event != null) { builder.put("event", event); builder.put("time-taken", time - event.getStartedAt()); } return builder.build(); }
@Test public void testTwoEvents() { activeEvents.active("hello", new BaseEventInfo()); activeEvents.active("hello2", new BaseEventInfo()); assertThat(activeEvents.events(), is(2)); assertThat(activeEvents.size(), is(2)); }
@Test public void testTwoIdenticalEvents() { activeEvents.active("hello", new BaseEventInfo()); activeEvents.active("hello", new BaseEventInfo()); assertThat(activeEvents.events(), is(2)); assertThat(activeEvents.size(), is(1)); }
private Map wrapInMap(T event, ImmutableMap data) { Long time = System.currentTimeMillis(); DateFormat format = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z"); String formatted = format.format(time); String change = Optional.ofNullable(event) .map(e -> Long.toString(Runtime.getRuntime().freeMemory() - e.getFreeMemory())).orElse("unknown"); ImmutableMap map = ImmutableMap.of("completed", time, "completed-formated", formatted, "memory-change", change); ImmutableMap.Builder builder = ImmutableMap.builder().putAll(data).putAll(map); if (event != null) { builder.put("event", event); builder.put("time-taken", time - event.getStartedAt()); } return builder.build(); }
@Test public void testOneEvent() { activeEvents.active("hello", new BaseEventInfo()); assertThat(activeEvents.events(), is(1)); }
@Test public void testFinishedStringImmutableMap() { activeEvents.active("hello", new BaseEventInfo()); activeEvents.finished("hello", ImmutableMap.of("hello", "world")); assertThat(activeEvents.events(), is(1)); assertThat(activeEvents.size(), is(0)); assertThat(activeEvents.toString(), containsString("world")); }
@Test public void testFinishedString() { activeEvents.active("hello", new BaseEventInfo()); activeEvents.finished("hello"); assertThat(activeEvents.events(), is(1)); assertThat(activeEvents.size(), is(0)); }