Skip to content

Commit

Permalink
add mock and mocks functions
Browse files Browse the repository at this point in the history
  • Loading branch information
telent committed Apr 19, 2021
1 parent dc1d3e2 commit ce9f7b6
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions tests/pppoe-test.fnl
Expand Up @@ -4,50 +4,55 @@
(print x)
x)

(tset package.loaded :netdev
{
(lambda mock [pkg name f]
(if (= nil (. package.loaded pkg))
(tset package.loaded pkg {}))
(tset (. package.loaded pkg)
name f))

(lambda mocks [pkg ...]
(let [[n f & rst] [...]]
(mock pkg n f)
(if (next rst)
(mocks pkg (table.unpack rst)))))


(mocks :netdev
"find-device" (fn [name] { "name" name })
"device-name" (fn [dev] dev.name)
"link-up?" (fn [dev] true)
})
(tset package.loaded :ppp
{
)

(mocks :ppp
"up?" (fn [dev] true)
"find-device" (fn [name] { "name" name })
"device-name" (fn [dev] dev.name)
"ipstate-script" (fn [dev] (.. "/run/services/ipstate-" dev.name))
})
)

(var my-events [])

(tset package.loaded :process
{
"new-process" (fn [command]
(assert
(or
(command:find "ipstate-%ppp.")
(command:find "ifconfig"))
command)
{:command command :running false})
(mocks :process
"new-process"
(fn [command]
(assert
(or
(command:find "ipstate-%ppp.")
(command:find "ifconfig"))
command)
{:command command :running false})
"clock" (fn [p] (- 123456 (length my-events)))
"start-process" (fn [p] (set p.running true))
"stop-process" (fn [p] (set p.running false))
"running?" (fn [p] p.running)
})

;; (fn mock [package name f]
;; (tset package.loaded[package] name f))

(tset package.loaded :event
{
"next-event" (fn []
(fn []
(let [e (table.remove my-events 1)]
(if (= (type e) "function")
(or (e) true)
e))))
})
"running?" (fn [p] p.running))

(mock :event "next-event"
(fn []
(fn []
(let [e (table.remove my-events 1)]
(if (= (type e) "function")
(or (e) true)
e)))))

(local pppoe (require :pppoe))

Expand All @@ -57,18 +62,15 @@
(var joined false)
(var started false)
(set my-events [1 2 3 4 5 6 7 8 ])
(tset package.loaded.process
:join
(fn [p] (set joined true)))
(tset package.loaded.process
"start-process"
(fn [p] (set started true)))
(mock :process :join #(set joined true))
(mock :process "start-process" #(set started true))
(pppoe "eth0" "ppp0")
(assert joined "ifconfig process did not join")
(assert started "daemon did not start"))

(lambda backoff-increases-on-failure []
;; given the process is started

;; when it stops without achieving health
;; there is a delay before it can be restarted
)
Expand Down

0 comments on commit ce9f7b6

Please sign in to comment.