Skip to content

Commit

Permalink
client uuids
Browse files Browse the repository at this point in the history
  • Loading branch information
smith committed May 19, 2011
1 parent ef00bd6 commit 922205e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 29 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
@@ -1,5 +1,9 @@
# History

## 0.4.0

* Unique client ids

## 0.3.0 / 2011-05-02

* Background job support
Expand Down
3 changes: 3 additions & 0 deletions lib/job.js
@@ -1,5 +1,6 @@
var gearman = require("gearman"),
inherits = require("util").inherits,
uuid = require("node-uuid"),
util = require("./util"),
extend = util.extend,
debug = util.debug,
Expand All @@ -11,6 +12,7 @@ var gearman = require("gearman"),
Job = function (options) {
if (!(this instanceof Job)) { return new Job(options); }
extend(this, options);
this.id = this.id || uuid("binary");
this.client = this.client || gearman.createClient();
this.priority = this.priority || "normal";

Expand All @@ -23,6 +25,7 @@ exports.Job = Job;
Job.prototype.submit = function () {
var client = this.client,
data = {
id: this.id,
name: this.name,
data: this.data,
encoding: this.encoding
Expand Down
4 changes: 3 additions & 1 deletion lib/packet/requests.js
Expand Up @@ -6,6 +6,7 @@ function submitJob(options) {
options = options || {};
var data = options.data || 0,
name = options.name,
id = options.id || new Buffer([1]), // uuid
encoding = options.encoding;

if (typeof name !== "string") {
Expand All @@ -20,7 +21,8 @@ function submitJob(options) {
return binary.put().
put(new Buffer(name, "ascii")).
word8(0).
word8(0). // TODO: unique client ids
put(id).
word8(0).
put(data).
buffer();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -10,7 +10,7 @@
"type": "git",
"url": "https://github.com/cramerdev/gearman-node.git"
},
"dependencies": { "binary": ">=0.0.0" },
"dependencies": { "binary": ">=0", "node-uuid": ">=0" },
"devDependencies": { "nodeunit": "~0.4" },
"scripts": { "test": "nodeunit test" }
}
3 changes: 1 addition & 2 deletions test/test-job.js
Expand Up @@ -7,15 +7,14 @@ var gearman = require("gearman"),
// XXX: These need a real gearman server running on localhost:4730 and
// test/fixtures/worker.rb running. Need to make a mock server or something.

// gearman.debug = true;

client = gearman.createClient();
job = client.submitJob("test", "test", { encoding: "utf8" });

module.exports = testCase({
"Job": function (test) {
test.ok(job instanceof EventEmitter,
"Job instances are EventEmitters");
test.ok(job.id instanceof Buffer, "Job id is a Buffer");
test.equal("normal", job.priority, "default priority is normal");
test.done();
},
Expand Down
49 changes: 24 additions & 25 deletions test/test-packet.js
Expand Up @@ -16,63 +16,62 @@ exports["encode"] = function (test) {

exports["encode SUBMIT_JOB"] = function (test) {
// examples
// \0REQ-SUBMIT_JOB-6-test\0-\0/
var empty = new Buffer([0,0x52,0x45,0x51,0,0,0,0x7,0,0,0,0x6,0x74,0x65,0x73,0x74,0,0]),
// \0REQ-SUBMIT_JOB-7-test\0-\0-a/
data = new Buffer([0,0x52,0x45,0x51,0,0,0,0x7,0,0,0,0x7,0x74,0x65,0x73,0x74,0,0,0x61]);

test.throws(function () { packet.encode({ type: "SUBMIT_JOB" }); }, "job must have a name string");
test.deepEqual(packet.encode({ type: "SUBMIT_JOB", name: "test" }), empty);
test.deepEqual(packet.encode({ type: "SUBMIT_JOB", name: "test", data: "a", encoding: "utf8" }), data);
test.deepEqual(packet.encode({ type: "SUBMIT_JOB", name: "test", data: new Buffer([0x61]) }), data);
// \0REQ-SUBMIT_JOB-7-test-\0-1-\0
var empty = new Buffer([0,0x52,0x45,0x51,0,0,0,0x7,0,0,0,7,0x74,0x65,0x73,0x74,0,1,0]),
// \0REQ-SUBMIT_JOB-8-test-\0-1-\0-a
data = new Buffer([0,0x52,0x45,0x51,0,0,0,0x7,0,0,0,8,0x74,0x65,0x73,0x74,0,1,0,0x61]);
test.throws(function () { packet.encode({ type: "SUBMIT_JOB", id: null }); }, "job must have a name string");
test.deepEqual(packet.encode({ type: "SUBMIT_JOB", name: "test", id: null }), empty);
test.deepEqual(packet.encode({ type: "SUBMIT_JOB", name: "test", data: "a", encoding: "utf8", id: null }), data);
test.deepEqual(packet.encode({ type: "SUBMIT_JOB", name: "test", data: new Buffer([0x61]), id: null }), data);
test.done();
};

exports["encode SUBMIT_JOB_HIGH"] = function (test) {
// \0REQ-SUBMIT_JOB_HIGH-7-test\0-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,21,0,0,0,7,0x74,0x65,0x73,0x74,0,0,0x61]);
// \0REQ-SUBMIT_JOB_HIGH-8-test\0-1-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,21,0,0,0,8,0x74,0x65,0x73,0x74,0,1,0,0x61]);

test.deepEqual(packet.encode({ type: "SUBMIT_JOB_HIGH", name: "test", data: "a", encoding: "utf8" }), data);
test.deepEqual(packet.encode({ type: "SUBMIT_JOB_HIGH", name: "test", data: "a", encoding: "utf8", id: null }), data);
test.done();
};

exports["encode SUBMIT_JOB_LOW"] = function (test) {
// \0REQ-SUBMIT_JOB_LOW-7-test\0-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,33,0,0,0,7,0x74,0x65,0x73,0x74,0,0,0x61]);
// \0REQ-SUBMIT_JOB_LOW-8-test\0-1-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,33,0,0,0,8,0x74,0x65,0x73,0x74,0,1,0,0x61]);

test.deepEqual(packet.encode({ type: "SUBMIT_JOB_LOW", name: "test", data: "a", encoding: "utf8" }), data);
test.deepEqual(packet.encode({ type: "SUBMIT_JOB_LOW", name: "test", data: "a", encoding: "utf8", id: null }), data);
test.done();
};

exports["encode SUBMIT_JOB_BG"] = function (test) {
// \0REQ-SUBMIT_JOB_BG-7-test\0-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,18,0,0,0,7,0x74,0x65,0x73,0x74,0,0,0x61]);
// \0REQ-SUBMIT_JOB_BG-8-test\0-1-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,18,0,0,0,8,0x74,0x65,0x73,0x74,0,1,0,0x61]);

test.deepEqual(packet.encode({ type: "SUBMIT_JOB_BG", name: "test", data: "a", encoding: "utf8" }), data);
test.deepEqual(packet.encode({ type: "SUBMIT_JOB_BG", name: "test", data: "a", encoding: "utf8", id: null }), data);
test.done();
};

exports["encode SUBMIT_JOB_HIGH_BG"] = function (test) {
// \0REQ-SUBMIT_JOB_HIGH_BG-7-test\0-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,32,0,0,0,7,0x74,0x65,0x73,0x74,0,0,0x61]);
// \0REQ-SUBMIT_JOB_HIGH_BG-8-test\0-1-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,32,0,0,0,8,0x74,0x65,0x73,0x74,0,1,0,0x61]);

test.deepEqual(packet.encode({ type: "SUBMIT_JOB_HIGH_BG", name: "test", data: "a", encoding: "utf8" }), data);
test.deepEqual(packet.encode({ type: "SUBMIT_JOB_HIGH_BG", name: "test", data: "a", encoding: "utf8", id: null }), data);
test.done();
};

exports["encode SUBMIT_JOB_LOW_BG"] = function (test) {
// \0REQ-SUBMIT_JOB_LOW_BG-7-test\0-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,34,0,0,0,7,0x74,0x65,0x73,0x74,0,0,0x61]);
// \0REQ-SUBMIT_JOB_LOW_BG-8-test\0-1-\0-a/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,34,0,0,0,8,0x74,0x65,0x73,0x74,0,1,0,0x61]);

test.deepEqual(packet.encode({ type: "SUBMIT_JOB_LOW_BG", name: "test", data: "a", encoding: "utf8" }), data);
test.deepEqual(packet.encode({ type: "SUBMIT_JOB_LOW_BG", name: "test", data: "a", encoding: "utf8", id: null }), data);
test.done();
};

exports["encode GET_STATUS"] = function (test) {
// \0REQ-GET_STATUS-5-test\0/
var data = new Buffer([0,0x52,0x45,0x51,0,0,0,15,0,0,0,4,0x74,0x65,0x73,0x74]);

test.deepEqual(packet.encode({ type: "GET_STATUS", handle: "test" }), data);
test.deepEqual(packet.encode({ type: "GET_STATUS", handle: "test", id: null }), data);
test.done();
};

Expand Down

0 comments on commit 922205e

Please sign in to comment.