Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not emit error when callback is passed to Advertisement.create #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/advertisement.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,20 @@ function Advertisement(serviceType, port, options, callback) {
serviceType, domain, context)
{
var error = dns_sd.buildException(errorCode);
if (callback) {
callback.call(self, error, {
var service;

if (!error) {
service = {
name: name
, type: makeServiceType(serviceType)
, domain: domain
, flags: flags
}, context);
};
}
if (error) {

if (callback) {
callback(error, service, context);
} else if (error) {
self.emit('error', error);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
, "ncp": "*"
, "minimatch": "*"
, "proxyquire": "~0.5"
, "sinon": "~1.14.1"
}
, "repository":
{ "type": "git"
Expand Down Expand Up @@ -61,4 +62,3 @@
}
, "gypfile": true
}

71 changes: 71 additions & 0 deletions tests/test_advert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var st = require('../lib/service_type.js')
, dns_sd = require('../lib/dns_sd')
, proxyquire = require('proxyquire')
, sinon = require('sinon')
;

//=== Browser ===========================================================

module.exports["Advertisement"] = {
"Should pass error to callback when error occurs during service registration": function(test) {
var callback = null
, invocations = 0
, threwException = false
, serviceType = "foo"
, port = "bar"
, options = "baz"
, error = new Error("Fail!")
, code = 10;

var mock_dns_sd = {
buildException: sinon.stub().withArgs(code).returns(error),
DNSServiceRegister: sinon.stub().callsArgWithAsync(9, null, null, code, null, null, null, null)
};

var mock_service_type = {
makeServiceType: sinon.stub()
}

// create the browser with our mock of dns_sd
var ad = proxyquire('../lib/advertisement.js', {
'./dns_sd': mock_dns_sd,
'./service_type': mock_service_type
}).Advertisement.create(serviceType, port, options, function(er) {
test.equal(error, er);

test.done();
});
},

"Should emit an error when when error occurs during service registration and no callbacked is passed": function(test) {
var callback = null
, invocations = 0
, threwException = false
, serviceType = "foo"
, port = "bar"
, options = "baz"
, error = new Error("Fail!")
, code = 10;

var mock_dns_sd = {
buildException: sinon.stub().withArgs(code).returns(error),
DNSServiceRegister: sinon.stub().callsArgWithAsync(9, null, null, code, null, null, null, null)
};

var mock_service_type = {
makeServiceType: sinon.stub()
}

// create the browser with our mock of dns_sd
var ad = proxyquire('../lib/advertisement.js', {
'./dns_sd': mock_dns_sd,
'./service_type': mock_service_type
}).Advertisement.create(serviceType, port, options)

ad.on('error', function(er) {
test.equal(error, er);

test.done();
});
}
}