|
4 | 4 | # Copyright, 2026, by Samuel Williams. |
5 | 5 |
|
6 | 6 | require "sus" |
7 | | -require "sus/fixtures/console/captured_logger" |
8 | 7 | require "sus/fixtures/temporary_directory_context" |
9 | 8 | require "async/utilization" |
10 | 9 | require "fileutils" |
11 | 10 |
|
12 | 11 | describe Async::Utilization::Observer do |
13 | | - include Sus::Fixtures::Console::CapturedLogger |
14 | 12 | include Sus::Fixtures::TemporaryDirectoryContext |
15 | 13 |
|
16 | 14 | let(:shm_path) {File.join(root, "test.shm")} |
|
41 | 39 | expect(observer.schema).to be == schema |
42 | 40 | end |
43 | 41 |
|
44 | | - it "can write values to shared memory" do |
45 | | - observer.set(:total_requests, 42) |
46 | | - observer.set(:active_requests, 5) |
47 | | - |
48 | | - # Read back from file to verify |
49 | | - buffer = IO::Buffer.map(File.open(shm_path, "r+b"), file_size, 0) |
50 | | - expect(buffer.get_value(:u64, 0)).to be == 42 |
51 | | - expect(buffer.get_value(:u32, 8)).to be == 5 |
52 | | - end |
53 | | - |
54 | 42 | with "non-page-aligned offsets" do |
55 | 43 | let(:file_size) {IO::Buffer::PAGE_SIZE * 2} |
56 | 44 | let(:offset) {100} # Not page-aligned |
57 | 45 |
|
58 | | - it "handles non-page-aligned offsets" do |
59 | | - observer.set(:total_requests, 100) |
60 | | - observer.set(:active_requests, 20) |
| 46 | + it "maps values at the correct offset" do |
| 47 | + observer.buffer.set_value(:u64, schema[:total_requests].offset, 100) |
| 48 | + observer.buffer.set_value(:u32, schema[:active_requests].offset, 20) |
61 | 49 |
|
62 | | - # Read back from file at the correct offset |
| 50 | + # Read back from file at the correct byte position |
63 | 51 | buffer = IO::Buffer.map(File.open(shm_path, "r+b"), file_size, 0) |
64 | | - expect(buffer.get_value(:u64, offset)).to be == 100 |
65 | | - expect(buffer.get_value(:u32, offset + 8)).to be == 20 |
| 52 | + expect(buffer.get_value(:u64, offset + schema[:total_requests].offset)).to be == 100 |
| 53 | + expect(buffer.get_value(:u32, offset + schema[:active_requests].offset)).to be == 20 |
66 | 54 | end |
67 | 55 | end |
68 | 56 |
|
69 | | - it "ignores fields not in schema" do |
70 | | - # Should not raise an error |
71 | | - observer.set(:unknown_field, 999) |
72 | | - |
73 | | - # Verify nothing was written |
74 | | - buffer = IO::Buffer.map(File.open(shm_path, "r+b"), file_size, 0) |
75 | | - expect(buffer.get_value(:u64, 0)).to be == 0 |
76 | | - end |
77 | | - |
78 | 57 | with "page-aligned offsets" do |
79 | 58 | let(:file_size) {page_size * 2} |
80 | 59 | let(:segment_size) {page_size} |
81 | 60 |
|
82 | | - it "handles page-aligned offsets without slicing" do |
83 | | - expect(observer).to be_a(Async::Utilization::Observer) |
84 | | - observer.set(:total_requests, 123) |
| 61 | + it "maps values at the correct offset" do |
| 62 | + observer.buffer.set_value(:u64, schema[:total_requests].offset, 123) |
85 | 63 |
|
86 | | - # Verify value was written |
87 | 64 | buffer = IO::Buffer.map(File.open(shm_path, "r+b"), file_size, 0) |
88 | | - expect(buffer.get_value(:u64, 0)).to be == 123 |
| 65 | + expect(buffer.get_value(:u64, schema[:total_requests].offset)).to be == 123 |
89 | 66 | end |
90 | 67 | end |
91 | | - |
92 | | - it "handles errors gracefully when setting values" do |
93 | | - # Create an invalid buffer that will cause an error |
94 | | - # We'll mock the buffer to raise an error |
95 | | - buffer = observer.instance_variable_get(:@buffer) |
96 | | - expect(buffer).to receive(:set_value).and_raise(IOError, "Buffer error") |
97 | | - |
98 | | - # Should not raise, but log a warning |
99 | | - observer.set(:total_requests, 42) |
100 | | - |
101 | | - # Assert that a warning was logged |
102 | | - expect_console.to have_logged( |
103 | | - severity: be == :warn, |
104 | | - subject: be_a(Async::Utilization::Observer), |
105 | | - message: be == "Failed to set field in shared memory!" |
106 | | - ) |
107 | | - end |
108 | 68 | end |
0 commit comments