|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +# |
| 5 | +# Copyright (c) 2017-2019, Matteo Cafasso. |
| 6 | +# All rights reserved. |
| 7 | + |
| 8 | +defmodule RabbitMQ.MessageDeduplicationPlugin.CacheManager.Test do |
| 9 | + use ExUnit.Case |
| 10 | + |
| 11 | + alias :timer, as: Timer |
| 12 | + alias :mnesia, as: Mnesia |
| 13 | + alias RabbitMQ.MessageDeduplicationPlugin.Cache, as: Cache |
| 14 | + alias RabbitMQ.MessageDeduplicationPlugin.CacheManager, as: CacheManager |
| 15 | + |
| 16 | + @caches :message_deduplication_caches |
| 17 | + |
| 18 | + setup do |
| 19 | + start_supervised!(%{id: :cache_manager, |
| 20 | + start: {CacheManager, |
| 21 | + :start_link, |
| 22 | + []}}) |
| 23 | + |
| 24 | + %{} |
| 25 | + end |
| 26 | + |
| 27 | + test "cache creation", %{} do |
| 28 | + options = [persistence: :memory] |
| 29 | + |
| 30 | + CacheManager.create(:cache, options) |
| 31 | + {:atomic, [:cache]} = Mnesia.transaction(fn -> Mnesia.all_keys(@caches) end) |
| 32 | + CacheManager.destroy(:cache) |
| 33 | + end |
| 34 | + |
| 35 | + test "cache deletion", %{} do |
| 36 | + options = [persistence: :memory] |
| 37 | + |
| 38 | + :ok = CacheManager.create(:cache, options) |
| 39 | + {:atomic, [:cache]} = Mnesia.transaction(fn -> Mnesia.all_keys(@caches) end) |
| 40 | + :ok = CacheManager.destroy(:cache) |
| 41 | + {:atomic, []} = Mnesia.transaction(fn -> Mnesia.all_keys(@caches) end) |
| 42 | + end |
| 43 | + |
| 44 | + test "cache cleanup routine", %{} do |
| 45 | + options = [persistence: :memory] |
| 46 | + |
| 47 | + :ok = CacheManager.create(:cache, options) |
| 48 | + |
| 49 | + {:ok, :inserted} = Cache.insert(:cache, "foo", 1000) |
| 50 | + |
| 51 | + Timer.sleep(3200) |
| 52 | + |
| 53 | + {:atomic, []} = Mnesia.transaction(fn -> Mnesia.all_keys(:cache) end) |
| 54 | + |
| 55 | + :ok = CacheManager.destroy(:cache) |
| 56 | + end |
| 57 | + |
| 58 | +end |
0 commit comments