1 | #include <gtest/gtest.h> |
2 | #include <memory> |
3 | |
4 | #include <base/system.h> |
5 | |
6 | #include <engine/client/serverbrowser_ping_cache.h> |
7 | #include <engine/console.h> |
8 | #include <engine/engine.h> |
9 | #include <engine/shared/config.h> |
10 | #include <engine/storage.h> |
11 | #include <test/test.h> |
12 | |
13 | TEST(ServerBrowser, PingCache) |
14 | { |
15 | CTestInfo Info; |
16 | Info.m_DeleteTestStorageFilesOnSuccess = true; |
17 | |
18 | auto pConsole = CreateConsole(FlagMask: CFGFLAG_CLIENT); |
19 | auto pStorage = std::unique_ptr<IStorage>(Info.CreateTestStorage()); |
20 | auto pPingCache = std::unique_ptr<IServerBrowserPingCache>(CreateServerBrowserPingCache(pConsole: pConsole.get(), pStorage: pStorage.get())); |
21 | |
22 | NETADDR Localhost4, Localhost6, OtherLocalhost4, OtherLocalhost6; |
23 | ASSERT_FALSE(net_addr_from_str(&Localhost4, "127.0.0.1:8303" )); |
24 | ASSERT_FALSE(net_addr_from_str(&Localhost6, "[::1]:8304" )); |
25 | ASSERT_FALSE(net_addr_from_str(&OtherLocalhost4, "127.0.0.1:8305" )); |
26 | ASSERT_FALSE(net_addr_from_str(&OtherLocalhost6, "[::1]:8306" )); |
27 | EXPECT_LT(net_addr_comp(&Localhost4, &Localhost6), 0); |
28 | NETADDR aLocalhostBoth[2] = {Localhost4, Localhost6}; |
29 | |
30 | EXPECT_EQ(pPingCache->NumEntries(), 0); |
31 | EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), -1); |
32 | EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), -1); |
33 | EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), -1); |
34 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), -1); |
35 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), -1); |
36 | |
37 | pPingCache->Load(); |
38 | |
39 | EXPECT_EQ(pPingCache->NumEntries(), 0); |
40 | EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), -1); |
41 | EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), -1); |
42 | EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), -1); |
43 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), -1); |
44 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), -1); |
45 | |
46 | // Newer pings overwrite older. |
47 | pPingCache->CachePing(Addr: Localhost4, Ping: 123); |
48 | pPingCache->CachePing(Addr: Localhost4, Ping: 234); |
49 | pPingCache->CachePing(Addr: Localhost4, Ping: 345); |
50 | pPingCache->CachePing(Addr: Localhost4, Ping: 456); |
51 | pPingCache->CachePing(Addr: Localhost4, Ping: 567); |
52 | pPingCache->CachePing(Addr: Localhost4, Ping: 678); |
53 | pPingCache->CachePing(Addr: Localhost4, Ping: 789); |
54 | pPingCache->CachePing(Addr: Localhost4, Ping: 890); |
55 | pPingCache->CachePing(Addr: Localhost4, Ping: 901); |
56 | pPingCache->CachePing(Addr: Localhost4, Ping: 135); |
57 | pPingCache->CachePing(Addr: Localhost4, Ping: 246); |
58 | pPingCache->CachePing(Addr: Localhost4, Ping: 357); |
59 | pPingCache->CachePing(Addr: Localhost4, Ping: 468); |
60 | pPingCache->CachePing(Addr: Localhost4, Ping: 579); |
61 | pPingCache->CachePing(Addr: Localhost4, Ping: 680); |
62 | pPingCache->CachePing(Addr: Localhost4, Ping: 791); |
63 | pPingCache->CachePing(Addr: Localhost4, Ping: 802); |
64 | pPingCache->CachePing(Addr: Localhost4, Ping: 913); |
65 | |
66 | EXPECT_EQ(pPingCache->NumEntries(), 1); |
67 | EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), 913); |
68 | EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), -1); |
69 | EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), 913); |
70 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), 913); |
71 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), -1); |
72 | |
73 | pPingCache->CachePing(Addr: Localhost4, Ping: 234); |
74 | pPingCache->CachePing(Addr: Localhost6, Ping: 345); |
75 | EXPECT_EQ(pPingCache->NumEntries(), 2); |
76 | EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), 234); |
77 | EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), 345); |
78 | EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), 234); |
79 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), 234); |
80 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), 345); |
81 | |
82 | // Port doesn't matter for overwriting. |
83 | pPingCache->CachePing(Addr: Localhost4, Ping: 1337); |
84 | EXPECT_EQ(pPingCache->NumEntries(), 2); |
85 | EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), 1337); |
86 | EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), 345); |
87 | EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), 345); |
88 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), 1337); |
89 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), 345); |
90 | |
91 | pPingCache.reset(p: CreateServerBrowserPingCache(pConsole: pConsole.get(), pStorage: pStorage.get())); |
92 | |
93 | // Persistence. |
94 | pPingCache->Load(); |
95 | EXPECT_EQ(pPingCache->NumEntries(), 2); |
96 | EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), 1337); |
97 | EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), 345); |
98 | EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), 345); |
99 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), 1337); |
100 | EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), 345); |
101 | } |
102 | |