← Lua Academy

Using the ctx · 59 / 69

Verify writes and expiration

YOUR TASK

Make this happen

3 steps
  1. Include ttl = 60 and return the host success flag. The tests simulate acceptance and rejection; nothing is written to a real cache.

  2. Read the cases in lesson.test.lua, then choose Check solution to see what needs to change.

  3. Update subject.lua until every test passes. Keep the supplied tests unchanged.

Read the starter and tests

subject.lua

exports.save = function(key: string, value: string)
  ctx.cache.set({ key = key, value = value })
  return true
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")

t.it("writes once with a sixty-second TTL", function()
  t.mock.host("ctx.cache.set"):returns(true, nil)
  local s = subject.load()
  t.expect(s.save("job:42", "ready")):eq(true)
  t.expect(t.mock.spy("ctx.cache.set")):called_once()
  t.expect(t.mock.spy("ctx.cache.set"))
    :called_with({ key = "job:42", value = "ready", ttl = 60 })
end)
t.it("reports a rejected write", function()
  t.mock.host("ctx.cache.set"):returns(false, "quota exceeded")
  local s = subject.load()
  t.expect(s.save("job:42", "ready")):eq(false)
end)