Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to test memoized functions? #88

Closed
plasticrake opened this issue Sep 8, 2017 · 3 comments · May be fixed by godaddy/node-flipr-etcd#5 or maxdavidson/tribus#3
Closed

Best way to test memoized functions? #88

plasticrake opened this issue Sep 8, 2017 · 3 comments · May be fixed by godaddy/node-flipr-etcd#5 or maxdavidson/tribus#3
Assignees
Labels

Comments

@plasticrake
Copy link

plasticrake commented Sep 8, 2017

I'm trying to write a test to check that my function is using the cached value. From what I can tell the only exposed properties are __memoized__ and _has() and _get().

Also many of my memoized functions take no arguments and _has() and _get() don't seem to work on those.

var fn = (a) => { return a;};
var mfn = require('memoizee')(fn);
mfn._has(); // false
mfn('test');
mfn._has('test'); // true
var fn = () => { return 'test';};
var mfn = require('memoizee')(fn);
mfn._has(); // **Uncaught TypeError: get is not a function(…)**
mfn();
mfn._has(); // **Uncaught TypeError: get is not a function(…)**

Is there an example on the best way to test this? I looked at some of the test scripts here but it looked like it kept a 'cache miss' counter in the original function.

@medikoo medikoo self-assigned this Sep 11, 2017
@medikoo
Copy link
Owner

medikoo commented Sep 11, 2017

@plasticrake Firstly, tests confirming that memoization works as expected are included in memoizee. I don't think it make sense to double them in any utilities that rely on memoizee.

Still I understand that you may want to conduct some sanity check, to confirm that indeed given function is memoized (so wrappped with memoizee or any other memoization provider).

Doing that library agnostic way would be to just run function twice and configure tests against observed side effects. Here I'm not sure what exactly your function does, so I'm not sure how such test should look on your side

Or if that for some reason is difficult (still I would wonder why?), then you may test it in less agnostic way as e.g.:

fn.__memoized__ === true

I'm closing it, but you still have some doubts let's continue discussion here

@medikoo medikoo closed this as completed Sep 11, 2017
@epayet
Copy link
Contributor

epayet commented Sep 11, 2017

I think @plasticrake pointed out a real issue.

A few tests, running on node:8.4.0:

Everything works fine with this:

> var fn = (a) => { return a;};
> var mfn = require('memoizee')(fn);
> mfn.__memoized__
true
> mfn._has();
false
> mfn('test');
'test'
> mfn._has('test'); // true
true

But it crashes with the example he gave:

> var fn = () => { return 'test';};
> var mfn = require('memoizee')(fn);
> mfn.__memoized__
true
> mfn._has();
TypeError: get is not a function
    at Function.<anonymous> (/node_modules/memoizee/lib/configure-map.js:172:8)
    at repl:1:5
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:440:10)
    at emitOne (events.js:120:20)
    at REPLServer.emit (events.js:210:7)
    at REPLServer.Interface._onLine (readline.js:279:10)
> mfn();
'test'

It seems that the function get is undefined when the original function doesn't take any parameters?

@medikoo medikoo reopened this Sep 11, 2017
@medikoo medikoo added the bug label Sep 11, 2017
@medikoo
Copy link
Owner

medikoo commented Sep 11, 2017

Great thanks @epayet indeed, I didn't read as through the example

Fixed with 7cb1c7a

Published as v0.4.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants