Skip to content

Commit 372bcbf

Browse files
authored
Merge pull request #17 from godaddy/free_buffers
Add explicit buffer cleanup in specs
2 parents 5c0aeea + 9245bd6 commit 372bcbf

File tree

5 files changed

+45
-27
lines changed

5 files changed

+45
-27
lines changed

.github/workflows/build.yml

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ jobs:
1414
name: ${{ matrix.os }} ruby-${{ matrix.ruby }}
1515
strategy:
1616
matrix:
17-
os: [ ubuntu-20.04, macos-11 ]
18-
ruby: [ 2.5, 2.6, 2.7, 3.0, 3.1, 3.2 ]
17+
os: [ ubuntu-22.04, macos-15 ]
18+
ruby: [ 2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 4.0 ]
1919
fail-fast: false
2020

2121
steps:
22-
- uses: actions/checkout@v2
22+
- uses: actions/checkout@v4
2323

2424
- name: Setup Ruby
2525
uses: ruby/setup-ruby@v1
@@ -29,29 +29,25 @@ jobs:
2929
bundler-cache: true
3030

3131
- name: Setup Go
32-
uses: actions/setup-go@v2
32+
uses: actions/setup-go@v6.3.0
3333
with:
34-
go-version: '1.20.5'
34+
go-version: '1.24'
3535

3636
- name: Build Linux
37-
if: matrix.os == 'ubuntu-20.04'
37+
if: matrix.os == 'ubuntu-22.04'
3838
shell: bash
3939
run: |
40-
BUILD_DIR="tmp"
41-
mkdir $BUILD_DIR
42-
cp -R spec/support/libcobhandemo/* $BUILD_DIR
43-
cd tmp
44-
GOOS=linux GOARCH=amd64 go build -buildmode=c-shared -ldflags='-s -w' -o libcobhandemo-x64.so libcobhandemo.go
40+
mkdir -p tmp
41+
cd spec/support/libcobhandemo
42+
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -buildmode=c-shared -ldflags='-s -w' -o ../../../tmp/libcobhandemo-x64.so .
4543
4644
- name: Build Darwin
47-
if: matrix.os == 'macos-11'
45+
if: matrix.os == 'macos-15'
4846
shell: bash
4947
run: |
50-
BUILD_DIR="tmp"
51-
mkdir $BUILD_DIR
52-
cp -R spec/support/libcobhandemo/* $BUILD_DIR
53-
cd tmp
54-
GOOS=darwin GOARCH=amd64 go build -buildmode=c-shared -ldflags='-s -w' -o libcobhandemo-x64.dylib libcobhandemo.go
48+
mkdir -p tmp
49+
cd spec/support/libcobhandemo
50+
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -buildmode=c-shared -ldflags='-s -w' -o ../../../tmp/libcobhandemo-arm64.dylib .
5551
5652
- name: Run specs
5753
run: |
@@ -67,16 +63,16 @@ jobs:
6763
strategy:
6864
matrix:
6965
include:
70-
- { os: ubuntu-20.04, ruby: 3.2 }
66+
- { os: ubuntu-22.04, ruby: 3.2 }
7167
fail-fast: false
7268

7369
steps:
74-
- uses: actions/checkout@v2
70+
- uses: actions/checkout@v4
7571

7672
- name: Setup Go
77-
uses: actions/setup-go@v2
73+
uses: actions/setup-go@v6.3.0
7874
with:
79-
go-version: '1.20.5'
75+
go-version: '1.24'
8076

8177
- name: Install OS dependencies
8278
shell: bash
@@ -87,11 +83,9 @@ jobs:
8783
- name: Build Linux ARM64
8884
shell: bash
8985
run: |
90-
BUILD_DIR="tmp"
91-
mkdir $BUILD_DIR
92-
cp -R spec/support/libcobhandemo/* $BUILD_DIR
93-
cd tmp
94-
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc go build -v -buildmode=c-shared -ldflags='-s -w' -o libcobhandemo-arm64.so libcobhandemo.go
86+
mkdir -p tmp
87+
cd spec/support/libcobhandemo
88+
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc go build -v -buildmode=c-shared -ldflags='-s -w' -o ../../../tmp/libcobhandemo-arm64.so .
9589
9690
- name: Run specs and rubocop checks
9791
shell: bash

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ Style/BlockDelimiters:
2929

3030
Style/HashAsLastArrayItem:
3131
Enabled: false
32+
33+
Style/EmptyClassDefinition:
34+
Enabled: false

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ source 'https://rubygems.org'
55
# Specify your gem's dependencies in cobhan.gemspec
66
gemspec
77

8+
gem 'json', '~> 2.7.0' # Pin to avoid ARM64 compilation issues with 2.19.0
89
gem 'rake', '~> 13.0'
910
gem 'rspec', '~> 3.10.0'
1011
gem 'rubocop', '~> 1.7'

spec/cobhan_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def self.add_int32(first, second)
6565
expect {
6666
CobhanApp.add_int32(1, 2)
6767
}.to raise_error(NoMethodError) do |e|
68-
expect(e.message).to include("undefined method `addInt32'")
68+
expect(e.message).to include("undefined method `addInt32'").or include("undefined method 'addInt32'")
6969
end
7070

7171
CobhanApp.load_library(
@@ -83,6 +83,8 @@ def self.add_int32(first, second)
8383
expect(memory_pointer.get_int32(0)).to eq(input.bytesize)
8484
expect(memory_pointer.get_int32(Cobhan::SIZEOF_INT32)).to eq(0)
8585
expect(memory_pointer.get_bytes(Cobhan::BUFFER_HEADER_SIZE, input.bytesize)).to eq(input)
86+
ensure
87+
memory_pointer&.free
8688
end
8789
end
8890

@@ -92,6 +94,8 @@ def self.add_int32(first, second)
9294
output = CobhanApp.cbuffer_to_string(memory_pointer)
9395
expect(output).to eq(input)
9496
expect(output.encoding).to eq(Encoding::UTF_8)
97+
ensure
98+
memory_pointer&.free
9599
end
96100

97101
it 'returns a string from C buffer pointing to a temp file' do
@@ -112,6 +116,8 @@ def self.add_int32(first, second)
112116
output = CobhanApp.cbuffer_to_string(out_buffer)
113117
expect(output).to eq('A' * 2048)
114118
expect(output.encoding).to eq(Encoding::UTF_8)
119+
ensure
120+
[in_buffer, out_buffer].compact.each(&:free)
115121
end
116122
end
117123

@@ -131,6 +137,8 @@ def self.add_int32(first, second)
131137

132138
expect(CobhanApp.temp_to_string(buffer_ptr, length)).to eq('content')
133139
expect(File.exist?(file.path)).to eq(false)
140+
ensure
141+
buffer_ptr&.free
134142
end
135143
end
136144

@@ -140,6 +148,8 @@ def self.add_int32(first, second)
140148
expect(memory_pointer.get_int32(0)).to eq(10)
141149
expect(memory_pointer.get_int32(Cobhan::SIZEOF_INT32)).to eq(0)
142150
expect(memory_pointer.size).to eq(10 + Cobhan::BUFFER_HEADER_SIZE)
151+
ensure
152+
memory_pointer&.free
143153
end
144154
end
145155

@@ -149,6 +159,8 @@ def self.add_int32(first, second)
149159
memory_pointer = CobhanApp.int_to_buffer(number)
150160
expect(memory_pointer.get_int64(0)).to eq(number)
151161
expect(memory_pointer.size).to eq(Cobhan::SIZEOF_INT32 * 2)
162+
ensure
163+
memory_pointer&.free
152164
end
153165
end
154166

@@ -157,6 +169,8 @@ def self.add_int32(first, second)
157169
number = (2**63) - 1
158170
memory_pointer = CobhanApp.int_to_buffer(number)
159171
expect(CobhanApp.buffer_to_int(memory_pointer)).to eq(number)
172+
ensure
173+
memory_pointer&.free
160174
end
161175
end
162176
end

spec/support/cobhan_module.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def to_upper(input)
3939
raise 'Failed to convert toUpper' if result.negative?
4040

4141
FFI.cbuffer_to_string(out_buffer)
42+
ensure
43+
[in_buffer, out_buffer].compact.each(&:free)
4244
end
4345

4446
def filter_json(json_input, disallowed_value)
@@ -50,6 +52,8 @@ def filter_json(json_input, disallowed_value)
5052
raise 'Failed to filter json' if result.negative?
5153

5254
FFI.cbuffer_to_string(json_output_buffer)
55+
ensure
56+
[json_input_buffer, disallowed_value_buffer, json_output_buffer].compact.each(&:free)
5357
end
5458

5559
def base64_encode(input)
@@ -60,6 +64,8 @@ def base64_encode(input)
6064
raise 'Failed to base64 encode' if result.negative?
6165

6266
FFI.cbuffer_to_string(output_buffer)
67+
ensure
68+
[input_buffer, output_buffer].compact.each(&:free)
6369
end
6470

6571
def sleep_test(seconds)

0 commit comments

Comments
 (0)