A friend and I were extending Nginx by using lua scripts on it. Just in case you don’t know, to enable lua scripting at nginx you can use a lua module you can read more about how to push Nginx to its limits with Lua.
Anyway we were in a cycle:
- We did some lua coding.
- Restart nginx.
- Test it manually.
And this cycle was repeating until we have what we want. This was time consuming and pretty boring as well.
We then thought we could try to do some test unit with the scripts. And it was amazingly simple. We created a file called tests.lua and then we import the code we were using on nginx config.
package.path = package.path .. ";puppet/modules/nginx/functions.lua.erb" require("functions")
We also created a simple assertion handler which outputs function name when it fails or pass.
function should(assertive) local test_name = debug.getinfo(2, "n").name assert(assertive, test_name .. " FAILED!") print(test_name .. " OK!") end
Then we could create test suit to run.
function it_sorts_hls_playlist_by_bitrate() local unsorted_playlist = [[#EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1277952 stream_1248/playlist.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=356352 stream_348/playlist.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=485376 stream_474/playlist.m3u8]] local expected_sorted = [[#EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=356352 stream_348/playlist.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=485376 stream_474/playlist.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1277952 stream_1248/playlist.m3u8]] should(sort_bitrates(unsorted_playlist) == expected_sorted) end
I think that helped us speeding up a lot our cycle. Once again, by isolating a component and testing it, it’s a great way to make us productive.