Vertical Sync and Ubuntu 10.10
Ubuntu 10.10, with the Compiz desktop effects enabled, is a formidable user interface. One minor issue is that the graphics refresh ignores the vertical sync by default, leading to some tearing and visual ugliness.
There are three settings that I used to fix this up, with a marked improvement in visual quality. Note: With vertical sync enabled, the UI might feel just a few milliseconds more sluggish than before, because it will wait slightly until the next sync pulse to refresh. This takes a little getting used to, but the overall improvement in the feel of the UI can be worth it. e.g. No more flickering!
Setting 1 is the "Sync To VBlank" item in the CompizConfig Settings Manager:
Setting 2 is in the "X Server XVideo Settings" panel of the NVIDIA X Server Settings control panel.
Setting 3 is in the "OpenGL Settings" panel of the NVIDIA X Server Settings control panel.
Testing REST API with LearnBoost’s Tobi + Vows.js
I've been looking for a clean, framework-independent way of doing white-box API testing using Node.js. For a long while, the things that popped up when doing a quick scan of the Node Package Manager package lists weren't ticking all of the right boxes: zombie.js is going for a full browser simulation but doesn't provide a simple Browser.post() method (you have to use selectors to find the form.submit button and then fire a click() on it), Node's native http.Client is too low-level and doesn't do cookies, and various other http request wrappers weren't quite cutting it either. I think I've found a solution for this particular version of the problem: LearnBoost's Tobi combined with Vows.js is letting me do clean REST API testing, with a minimum of hassle, and all the built-in sugar-coated goodness of should.js fluent assertions. For example:
var HOST = 'localhost';
var PORT = 80;
var API = "/api/user/create";
var tobi = require('tobi'),
vows = require('vows'),
assert = require('assert');
var newbie = function() { return tobi.createBrowser(PORT, HOST) };
// -------------------------------------------------------------
// Macros.
// -------------------------------------------------------------
var macroPostOnlyApiChecks = function(url) {
return {
'GET': {
topic: function() {
var browser = newbie();
browser.get(url, this.callback);
},
'should fail.': function(res, $) {
res.should.not.have.status(200);
}
},
'Empty POST': {
topic: function() {
var browser = newbie();
browser.post(url, this.callback);
},
'should fail.': function(res, $) {
res.should.not.have.status(200);
}
}
};
};
var macroCreateUserOk = function(suite) {
return {
'Create user': {
topic: function() {
var browser = newbie();
var data = { signupUsername: '', };
data = JSON.stringify(data);
browser.post('/api/user/create', data, this.callback);
},
'should succeed.': function(res, $) {
res.should.have.status(200);
console.log(res);
// Pass created user credentials back to
// the calling suite for later use.
if (suite) {
}
}
}
};
};
// -------------------------------------------------------------
// User API Test Suite
//
// Run me with: vows api.user.create.vows.js --spec
// -------------------------------------------------------------
var suite = vows.describe('User API Test Suite');
// Batches are executed sequentially.
// Contexts are executed in parallel.
suite.addBatch(macroPostOnlyApiChecks(API));
suite.addBatch(macroCreateUserOk());
suite.export(module);
Future steps then include using the macros to help set up other tests that require a valid user, etc. Overall, this is the most straightforward solution I've yet found for the problem of testing a REST API while also faking a session.
Decorators In C
Turns out you can also do decorators in C, this came in handy for something else I had to work on recently. It's not quite as nice, because functions aren't first-class, but handy nonetheless.
Here's what that looks like:
#includetypedef int (* const decorableFnType)(int paramOne, int paramTwo); int functionOne(int paramOne, int paramTwo) { printf("functionOne: %d, %d\n", paramOne, paramTwo); return 0; } int functionTwo(int paramOne, int paramTwo) { printf("functionTwo: %d, %d\n", paramOne, paramTwo); return 0; } int functionThree(int paramOne, int paramTwo) { printf("functionThree: %d, %d\n", paramOne, paramTwo); return 0; } int decorator(int paramOne, int paramTwo, decorableFnType originalFn) { printf("decorator precondition!\n"); originalFn(paramOne, paramTwo); return 0; } int main(int argc, char **argv) { printf("hello, C decorators!\n"); functionOne(1, 2); decorator(1, 2, functionOne); decorator(1, 2, functionTwo); decorator(1, 2, functionThree); return 0; }
The output of which is:
vilimpoc@funky:~$ ./c-decorators
hello, C decorators!
functionOne: 1, 2
decorator precondition!
functionOne: 1, 2
decorator precondition!
functionTwo: 1, 2
decorator precondition!
functionThree: 1, 2
vilimpoc@funky:~$
This might be a bit simplistic, in reality, you'd probably want to decorate a function with a signature like the following, so that you can just change the structure and not bother w/a bajillion function declarations:
typedef int (* const decorableFnType)(SomeStruct * data);
Again, not super elegant, but handy.
Javascript Decorators
One reason why Javascript rocks:
As a use case, imagine you want to restrict a certain set of functions to only run if you are logged in. Doing stuff like this is ridiculously easy with first-class functions.
Here's a generic decoration example:
(function()
{
var original = function(paramOne, paramTwo) {
console.log('original: ' + paramOne + ' ' + paramTwo);
};
var decorator = function(originalFn) {
return function(paramOne, paramTwo) {
console.log('decorator: ' + paramOne + ' ' + paramTwo);
originalFn(paramOne, paramTwo);
};
};
var fnTable = {};
var registerCallback = function(url, callback) {
fnTable[url] = callback;
};
registerCallback('/abc', original);
registerCallback('/def', decorator(original));
fnTable['/abc']('one', 'two');
fnTable['/def']('three', 'four');
})();
Update: Here's the specific way you'd do this with node.js/Express:
Since all of the URL handlers you register have the same signature, it's easy to add precondition checks to the handlers via decorators.
var decorator = function(originalFn) {
return function(req, res) {
if (req.session.userLoggedIn) {
originalFn(req, res);
} else {
redirectSomewhere(res);
}
}
}
app.get('/url', decorator(original));
app.post('/other', decorator(original));
You get preconditions essentially for free, which is a damn sight better than adding the if() block to each and every handler function. Also, if you need more preconditions in the future, you can just stack them.


