Skip to content

Commit bba9b02

Browse files
committed
Merge pull request #44 from drewish/safe-double-close
Avoid fatal error when closing unopened port
2 parents 7732180 + e95e8d6 commit bba9b02

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

src/node-midi.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ class NodeMidiInput : public ObjectWrap
309309
{
310310
v8::HandleScope scope;
311311
NodeMidiInput* input = ObjectWrap::Unwrap<NodeMidiInput>(args.This());
312-
input->Unref();
312+
if (input->in->isPortOpen()) {
313+
input->Unref();
314+
}
313315
input->in->closePort();
314316
uv_close((uv_handle_t*)&input->message_async, NULL);
315317
return scope.Close(v8::Undefined());

test/unit-tests.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,52 @@ describe('midi.input', function() {
4141
input.getPortName(999).should.eql('');
4242
});
4343
});
44+
45+
describe('.openPort', function() {
46+
var input = new Midi.input();
47+
48+
it('requires an argument', function() {
49+
(function() {
50+
input.openPort();
51+
}).should.throw('First argument must be an integer');
52+
});
53+
54+
it('requires an integer', function() {
55+
(function() {
56+
input.openPort('asdf');
57+
}).should.throw('First argument must be an integer');
58+
});
59+
60+
it('requires a valid port', function() {
61+
(function() {
62+
input.openPort(999);
63+
}).should.throw('Invalid MIDI port number');
64+
});
65+
});
66+
67+
describe('.openVirtualPort', function() {
68+
var input = new Midi.input();
69+
70+
it('requires an argument', function() {
71+
(function() {
72+
input.openVirtualPort();
73+
}).should.throw('First argument must be a string');
74+
});
75+
76+
it('requires a string', function() {
77+
(function() {
78+
input.openVirtualPort(999);
79+
}).should.throw('First argument must be a string');
80+
});
81+
});
82+
83+
describe('.closePort', function() {
84+
var input = new Midi.input();
85+
86+
it('allows you to close a port that was not opened', function() {
87+
input.closePort();
88+
});
89+
});
4490
});
4591

4692
describe('midi.output', function() {
@@ -82,4 +128,50 @@ describe('midi.output', function() {
82128
output.getPortName(999).should.eql('');
83129
});
84130
});
131+
132+
describe('.openPort', function() {
133+
var output = new Midi.output();
134+
135+
it('requires an argument', function() {
136+
(function() {
137+
output.openPort();
138+
}).should.throw('First argument must be an integer');
139+
});
140+
141+
it('requires an integer', function() {
142+
(function() {
143+
output.openPort('asdf');
144+
}).should.throw('First argument must be an integer');
145+
});
146+
147+
it('requires a valid port', function() {
148+
(function() {
149+
output.openPort(999);
150+
}).should.throw('Invalid MIDI port number');
151+
});
152+
});
153+
154+
describe('.openVirtualPort', function() {
155+
var output = new Midi.output();
156+
157+
it('requires an argument', function() {
158+
(function() {
159+
output.openVirtualPort();
160+
}).should.throw('First argument must be a string');
161+
});
162+
163+
it('requires a string', function() {
164+
(function() {
165+
output.openVirtualPort(999);
166+
}).should.throw('First argument must be a string');
167+
});
168+
});
169+
170+
describe('.closePort', function() {
171+
var output = new Midi.output();
172+
173+
it('allows you to close a port that was not opened', function() {
174+
output.closePort();
175+
});
176+
});
85177
});

0 commit comments

Comments
 (0)