|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright (c) "Neo4j" |
| 5 | +# Neo4j Sweden AB [http://neo4j.com] |
| 6 | +# |
| 7 | +# This file is part of Neo4j. |
| 8 | +# |
| 9 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +# you may not use this file except in compliance with the License. |
| 11 | +# You may obtain a copy of the License at |
| 12 | +# |
| 13 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +# |
| 15 | +# Unless required by applicable law or agreed to in writing, software |
| 16 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +# See the License for the specific language governing permissions and |
| 19 | +# limitations under the License. |
| 20 | + |
| 21 | +import inspect |
| 22 | +from unittest.mock import Mock |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | +from ..work import FakeConnection |
| 27 | + |
| 28 | +from neo4j import READ_ACCESS |
| 29 | +from neo4j.addressing import ResolvedAddress |
| 30 | +from neo4j.conf import ( |
| 31 | + PoolConfig, |
| 32 | + RoutingConfig, |
| 33 | + WorkspaceConfig |
| 34 | +) |
| 35 | +from neo4j.io import Neo4jPool |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture() |
| 39 | +def opener(): |
| 40 | + def open_(*_, **__): |
| 41 | + connection = FakeConnection() |
| 42 | + route_mock = Mock() |
| 43 | + route_mock.return_value = [{ |
| 44 | + "ttl": 1000, |
| 45 | + "servers": [ |
| 46 | + {"addresses": ["1.2.3.1:9001"], "role": "ROUTE"}, |
| 47 | + { |
| 48 | + "addresses": ["1.2.3.10:9010", "1.2.3.11:9011"], |
| 49 | + "role": "READ" |
| 50 | + }, |
| 51 | + { |
| 52 | + "addresses": ["1.2.3.20:9020", "1.2.3.21:9021"], |
| 53 | + "role": "WRITE" |
| 54 | + }, |
| 55 | + ], |
| 56 | + }] |
| 57 | + connection.attach_mock(route_mock, "route") |
| 58 | + opener_.connections.append(connection) |
| 59 | + return connection |
| 60 | + |
| 61 | + opener_ = Mock() |
| 62 | + opener_.connections = [] |
| 63 | + opener_.side_effect = open_ |
| 64 | + return opener_ |
| 65 | + |
| 66 | + |
| 67 | +def test_acquires_new_routing_table_if_deleted(opener): |
| 68 | + address = ResolvedAddress(("1.2.3.1", 9001), host_name="host") |
| 69 | + pool = Neo4jPool(opener, PoolConfig(), WorkspaceConfig(), address) |
| 70 | + cx = pool.acquire(READ_ACCESS, 30, "test_db", None) |
| 71 | + pool.release(cx) |
| 72 | + assert pool.routing_tables.get("test_db") |
| 73 | + |
| 74 | + del pool.routing_tables["test_db"] |
| 75 | + |
| 76 | + cx = pool.acquire(READ_ACCESS, 30, "test_db", None) |
| 77 | + pool.release(cx) |
| 78 | + assert pool.routing_tables.get("test_db") |
| 79 | + |
| 80 | + |
| 81 | +def test_acquires_new_routing_table_if_stale(opener): |
| 82 | + address = ResolvedAddress(("1.2.3.1", 9001), host_name="host") |
| 83 | + pool = Neo4jPool(opener, PoolConfig(), WorkspaceConfig(), address) |
| 84 | + cx = pool.acquire(READ_ACCESS, 30, "test_db", None) |
| 85 | + pool.release(cx) |
| 86 | + assert pool.routing_tables.get("test_db") |
| 87 | + |
| 88 | + old_value = pool.routing_tables["test_db"].last_updated_time |
| 89 | + pool.routing_tables["test_db"].ttl = 0 |
| 90 | + |
| 91 | + cx = pool.acquire(READ_ACCESS, 30, "test_db", None) |
| 92 | + pool.release(cx) |
| 93 | + assert pool.routing_tables["test_db"].last_updated_time > old_value |
| 94 | + |
| 95 | + |
| 96 | +def test_removes_old_routing_table(opener): |
| 97 | + address = ResolvedAddress(("1.2.3.1", 9001), host_name="host") |
| 98 | + pool = Neo4jPool(opener, PoolConfig(), WorkspaceConfig(), address) |
| 99 | + cx = pool.acquire(READ_ACCESS, 30, "test_db1", None) |
| 100 | + pool.release(cx) |
| 101 | + assert pool.routing_tables.get("test_db1") |
| 102 | + cx = pool.acquire(READ_ACCESS, 30, "test_db2", None) |
| 103 | + pool.release(cx) |
| 104 | + assert pool.routing_tables.get("test_db2") |
| 105 | + |
| 106 | + old_value = pool.routing_tables["test_db1"].last_updated_time |
| 107 | + pool.routing_tables["test_db1"].ttl = 0 |
| 108 | + pool.routing_tables["test_db2"].ttl = \ |
| 109 | + -RoutingConfig.routing_table_purge_delay |
| 110 | + |
| 111 | + cx = pool.acquire(READ_ACCESS, 30, "test_db1", None) |
| 112 | + pool.release(cx) |
| 113 | + assert pool.routing_tables["test_db1"].last_updated_time > old_value |
| 114 | + assert "test_db2" not in pool.routing_tables |
| 115 | + |
0 commit comments