mudis 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +958 -932
- data/lib/mudis/expiry.rb +53 -0
- data/lib/mudis/lru.rb +66 -0
- data/lib/mudis/metrics.rb +42 -0
- data/lib/mudis/namespace.rb +47 -0
- data/lib/mudis/persistence.rb +113 -0
- data/lib/mudis/version.rb +3 -3
- data/lib/mudis.rb +370 -631
- data/lib/mudis_client.rb +107 -65
- data/lib/mudis_config.rb +35 -35
- data/lib/mudis_ipc_config.rb +13 -0
- data/lib/mudis_proxy.rb +39 -37
- data/lib/mudis_server.rb +100 -81
- data/sig/mudis.rbs +62 -56
- data/sig/mudis_client.rbs +24 -22
- data/sig/mudis_config.rbs +10 -10
- data/sig/mudis_expiry.rbs +13 -0
- data/sig/mudis_ipc_config.rbs +10 -0
- data/sig/mudis_lru.rbs +21 -0
- data/sig/mudis_metrics.rbs +11 -0
- data/sig/mudis_namespace.rbs +13 -0
- data/sig/mudis_persistence.rbs +19 -0
- data/sig/mudis_server.rbs +16 -6
- data/spec/api_compatibility_spec.rb +155 -0
- data/spec/eviction_spec.rb +29 -29
- data/spec/guardrails_spec.rb +138 -138
- data/spec/memory_guard_spec.rb +33 -33
- data/spec/metrics_spec.rb +34 -34
- data/spec/modules/expiry_spec.rb +170 -0
- data/spec/modules/lru_spec.rb +149 -0
- data/spec/modules/metrics_spec.rb +105 -0
- data/spec/modules/namespace_spec.rb +157 -0
- data/spec/modules/persistence_spec.rb +125 -0
- data/spec/mudis_client_spec.rb +147 -137
- data/spec/mudis_server_spec.rb +100 -90
- data/spec/mudis_spec.rb +183 -183
- data/spec/namespace_spec.rb +69 -69
- data/spec/persistence_spec.rb +38 -37
- data/spec/reset_spec.rb +31 -31
- metadata +51 -3
data/spec/mudis_client_spec.rb
CHANGED
|
@@ -1,137 +1,147 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative "spec_helper"
|
|
4
|
-
|
|
5
|
-
RSpec.describe MudisClient do # rubocop:disable Metrics/BlockLength
|
|
6
|
-
let(:socket_path) {
|
|
7
|
-
let(:
|
|
8
|
-
let(:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
expect(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
expect { client.read("test_key") }.to
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe MudisClient do # rubocop:disable Metrics/BlockLength
|
|
6
|
+
let(:socket_path) { MudisIPCConfig::SOCKET_PATH }
|
|
7
|
+
let(:socket_class) { MudisIPCConfig.use_tcp? ? TCPSocket : UNIXSocket }
|
|
8
|
+
let(:mock_socket) { instance_double(socket_class) }
|
|
9
|
+
let(:client) { MudisClient.new }
|
|
10
|
+
|
|
11
|
+
around do |example|
|
|
12
|
+
ClimateControl.modify("SOCKET_PATH" => socket_path) do
|
|
13
|
+
example.run
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
before do
|
|
18
|
+
if MudisIPCConfig.use_tcp?
|
|
19
|
+
allow(TCPSocket).to receive(:new).and_return(mock_socket)
|
|
20
|
+
else
|
|
21
|
+
allow(UNIXSocket).to receive(:open).and_yield(mock_socket)
|
|
22
|
+
end
|
|
23
|
+
allow(mock_socket).to receive(:close)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe "#read" do
|
|
27
|
+
it "sends a read command and returns the value" do
|
|
28
|
+
payload = { cmd: "read", key: "test_key", namespace: nil }
|
|
29
|
+
response = { ok: true, value: "test_value" }.to_json
|
|
30
|
+
|
|
31
|
+
expect(mock_socket).to receive(:puts).with(payload.to_json)
|
|
32
|
+
expect(mock_socket).to receive(:gets).and_return(response)
|
|
33
|
+
|
|
34
|
+
expect(client.read("test_key")).to eq("test_value")
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
describe "#write" do
|
|
39
|
+
it "sends a write command and returns the value" do
|
|
40
|
+
payload = { cmd: "write", key: "test_key", value: "test_value", ttl: nil, namespace: nil }
|
|
41
|
+
response = { ok: true, value: nil }.to_json
|
|
42
|
+
|
|
43
|
+
expect(mock_socket).to receive(:puts).with(payload.to_json)
|
|
44
|
+
expect(mock_socket).to receive(:gets).and_return(response)
|
|
45
|
+
|
|
46
|
+
expect(client.write("test_key", "test_value")).to be_nil
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe "#delete" do
|
|
51
|
+
it "sends a delete command and returns the value" do
|
|
52
|
+
payload = { cmd: "delete", key: "test_key", namespace: nil }
|
|
53
|
+
response = { ok: true, value: nil }.to_json
|
|
54
|
+
|
|
55
|
+
expect(mock_socket).to receive(:puts).with(payload.to_json)
|
|
56
|
+
expect(mock_socket).to receive(:gets).and_return(response)
|
|
57
|
+
|
|
58
|
+
expect(client.delete("test_key")).to be_nil
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
describe "#exists?" do
|
|
63
|
+
it "sends an exists command and returns true" do
|
|
64
|
+
payload = { cmd: "exists", key: "test_key", namespace: nil }
|
|
65
|
+
response = { ok: true, value: true }.to_json
|
|
66
|
+
|
|
67
|
+
expect(mock_socket).to receive(:puts).with(payload.to_json)
|
|
68
|
+
expect(mock_socket).to receive(:gets).and_return(response)
|
|
69
|
+
|
|
70
|
+
expect(client.exists?("test_key")).to eq(true)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
describe "#fetch" do
|
|
75
|
+
it "fetches an existing value or writes a new one" do
|
|
76
|
+
read_response = { ok: true, value: nil }.to_json
|
|
77
|
+
write_payload = { cmd: "write", key: "test_key", value: "new_value", ttl: nil, namespace: nil }
|
|
78
|
+
write_response = { ok: true, value: nil }.to_json
|
|
79
|
+
|
|
80
|
+
expect(mock_socket).to receive(:puts).with({ cmd: "read", key: "test_key", namespace: nil }.to_json)
|
|
81
|
+
expect(mock_socket).to receive(:gets).and_return(read_response)
|
|
82
|
+
expect(mock_socket).to receive(:puts).with(write_payload.to_json)
|
|
83
|
+
expect(mock_socket).to receive(:gets).and_return(write_response)
|
|
84
|
+
|
|
85
|
+
result = client.fetch("test_key") { "new_value" } # rubocop:disable Style/RedundantFetchBlock
|
|
86
|
+
expect(result).to eq("new_value")
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
describe "#metrics" do
|
|
91
|
+
it "sends a metrics command and returns the metrics" do
|
|
92
|
+
payload = { cmd: "metrics" }
|
|
93
|
+
response = { ok: true, value: { reads: 10, writes: 5 } }.to_json
|
|
94
|
+
|
|
95
|
+
expect(mock_socket).to receive(:puts).with(payload.to_json)
|
|
96
|
+
expect(mock_socket).to receive(:gets).and_return(response)
|
|
97
|
+
|
|
98
|
+
expect(client.metrics).to eq({ reads: 10, writes: 5 })
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
describe "#reset_metrics!" do
|
|
103
|
+
it "sends a reset_metrics command" do
|
|
104
|
+
payload = { cmd: "reset_metrics" }
|
|
105
|
+
response = { ok: true, value: nil }.to_json
|
|
106
|
+
|
|
107
|
+
expect(mock_socket).to receive(:puts).with(payload.to_json)
|
|
108
|
+
expect(mock_socket).to receive(:gets).and_return(response)
|
|
109
|
+
|
|
110
|
+
expect(client.reset_metrics!).to be_nil
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
describe "#reset!" do
|
|
115
|
+
it "sends a reset command" do
|
|
116
|
+
payload = { cmd: "reset" }
|
|
117
|
+
response = { ok: true, value: nil }.to_json
|
|
118
|
+
|
|
119
|
+
expect(mock_socket).to receive(:puts).with(payload.to_json)
|
|
120
|
+
expect(mock_socket).to receive(:gets).and_return(response)
|
|
121
|
+
|
|
122
|
+
expect(client.reset!).to be_nil
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
describe "error handling" do
|
|
127
|
+
it "warns when the socket is missing" do
|
|
128
|
+
if MudisIPCConfig.use_tcp?
|
|
129
|
+
allow(TCPSocket).to receive(:new).and_raise(Errno::ECONNREFUSED)
|
|
130
|
+
else
|
|
131
|
+
allow(UNIXSocket).to receive(:open).and_raise(Errno::ENOENT)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
expect { client.read("test_key") }.to output(/Cannot connect/).to_stderr
|
|
135
|
+
expect(client.read("test_key")).to be_nil
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
it "raises an error when the server returns an error" do
|
|
139
|
+
response = { ok: false, error: "Something went wrong" }.to_json
|
|
140
|
+
|
|
141
|
+
expect(mock_socket).to receive(:puts)
|
|
142
|
+
expect(mock_socket).to receive(:gets).and_return(response)
|
|
143
|
+
|
|
144
|
+
expect { client.read("test_key") }.to raise_error("Something went wrong")
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
data/spec/mudis_server_spec.rb
CHANGED
|
@@ -1,90 +1,100 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "socket"
|
|
4
|
-
require "json"
|
|
5
|
-
|
|
6
|
-
require_relative "spec_helper"
|
|
7
|
-
|
|
8
|
-
RSpec.describe MudisServer do # rubocop:disable Metrics/BlockLength
|
|
9
|
-
let(:socket_path) {
|
|
10
|
-
|
|
11
|
-
before do
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
allow(Mudis).to receive(:
|
|
19
|
-
allow(Mudis).to receive(:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "socket"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
require_relative "spec_helper"
|
|
7
|
+
|
|
8
|
+
RSpec.describe MudisServer do # rubocop:disable Metrics/BlockLength
|
|
9
|
+
let(:socket_path) { MudisIPCConfig::SOCKET_PATH }
|
|
10
|
+
|
|
11
|
+
before(:all) do
|
|
12
|
+
# Start the server once for all tests
|
|
13
|
+
Thread.new { MudisServer.start! }
|
|
14
|
+
sleep 0.2 # Allow the server to start
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
before(:each) do
|
|
18
|
+
allow(Mudis).to receive(:read).and_return("mock_value")
|
|
19
|
+
allow(Mudis).to receive(:write)
|
|
20
|
+
allow(Mudis).to receive(:delete)
|
|
21
|
+
allow(Mudis).to receive(:exists?).and_return(true)
|
|
22
|
+
allow(Mudis).to receive(:fetch).and_return("mock_fetched_value")
|
|
23
|
+
allow(Mudis).to receive(:metrics).and_return({ reads: 1, writes: 1 })
|
|
24
|
+
allow(Mudis).to receive(:reset_metrics!)
|
|
25
|
+
allow(Mudis).to receive(:reset!)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
after do
|
|
29
|
+
File.unlink(socket_path) if File.exist?(socket_path) && !MudisIPCConfig.use_tcp?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def send_request(request)
|
|
33
|
+
if MudisIPCConfig.use_tcp?
|
|
34
|
+
TCPSocket.open(MudisIPCConfig::TCP_HOST, MudisIPCConfig::TCP_PORT) do |sock|
|
|
35
|
+
sock.puts(JSON.dump(request))
|
|
36
|
+
JSON.parse(sock.gets, symbolize_names: true)
|
|
37
|
+
end
|
|
38
|
+
else
|
|
39
|
+
UNIXSocket.open(socket_path) do |sock|
|
|
40
|
+
sock.puts(JSON.dump(request))
|
|
41
|
+
JSON.parse(sock.gets, symbolize_names: true)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "handles the 'read' command" do
|
|
47
|
+
response = send_request({ cmd: "read", key: "test_key", namespace: "test_ns" })
|
|
48
|
+
expect(response).to eq({ ok: true, value: "mock_value" })
|
|
49
|
+
expect(Mudis).to have_received(:read).with("test_key", namespace: "test_ns")
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "handles the 'write' command" do
|
|
53
|
+
response = send_request({ cmd: "write", key: "test_key", value: "test_value", ttl: 60, namespace: "test_ns" })
|
|
54
|
+
expect(response).to eq({ ok: true, value: nil })
|
|
55
|
+
expect(Mudis).to have_received(:write).with("test_key", "test_value", expires_in: 60, namespace: "test_ns")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "handles the 'delete' command" do
|
|
59
|
+
response = send_request({ cmd: "delete", key: "test_key", namespace: "test_ns" })
|
|
60
|
+
expect(response).to eq({ ok: true, value: nil })
|
|
61
|
+
expect(Mudis).to have_received(:delete).with("test_key", namespace: "test_ns")
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "handles the 'exists' command" do
|
|
65
|
+
response = send_request({ cmd: "exists", key: "test_key", namespace: "test_ns" })
|
|
66
|
+
expect(response).to eq({ ok: true, value: true })
|
|
67
|
+
expect(Mudis).to have_received(:exists?).with("test_key", namespace: "test_ns")
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "handles the 'fetch' command" do
|
|
71
|
+
response = send_request({ cmd: "fetch", key: "test_key", ttl: 60, namespace: "test_ns",
|
|
72
|
+
fallback: "fallback_value" })
|
|
73
|
+
expect(response).to eq({ ok: true, value: "mock_fetched_value" })
|
|
74
|
+
expect(Mudis).to have_received(:fetch).with("test_key", expires_in: 60, namespace: "test_ns")
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "handles the 'metrics' command" do
|
|
78
|
+
response = send_request({ cmd: "metrics" })
|
|
79
|
+
expect(response).to eq({ ok: true, value: { reads: 1, writes: 1 } })
|
|
80
|
+
expect(Mudis).to have_received(:metrics)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "handles the 'reset_metrics' command" do
|
|
84
|
+
response = send_request({ cmd: "reset_metrics" })
|
|
85
|
+
expect(response).to eq({ ok: true, value: nil })
|
|
86
|
+
expect(Mudis).to have_received(:reset_metrics!)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it "handles the 'reset' command" do
|
|
90
|
+
response = send_request({ cmd: "reset" })
|
|
91
|
+
expect(response).to eq({ ok: true, value: nil })
|
|
92
|
+
expect(Mudis).to have_received(:reset!)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it "handles unknown commands" do
|
|
96
|
+
response = send_request({ cmd: "unknown_command" })
|
|
97
|
+
expect(response[:ok]).to be false
|
|
98
|
+
expect(response[:error]).to match(/unknown command/i)
|
|
99
|
+
end
|
|
100
|
+
end
|