juan_gandhi: (Default)
Juan-Carlos Gandhi ([personal profile] juan_gandhi) wrote2020-08-23 09:18 pm

TWIMC: tests using random and current time

 So, if you think you call a function in your code, and this function returns current time, or a random number... IT'S NOT A FUNCTION. Your code is function of "random number", or "time".

So, if your code is written as something that retrieves this kind of data, to test your code, you should provide that data. Not just today, but try the time, like 10 years from now. As to "random", You provide the randomness. If your code cannot be fixed to behave as a function of those inputs, make your "random stream" or "time stream" not hard-coded, but substitutable. Mockable. And mock it in your tests. MAKE SURE that you don't provide just happy-path data. Provide anything. A sequence of 100 numbers 4 for random. Time that is 10 years from now. Or even 30 yeas from now.

Make sure that your tests don't depend on anything. Because test Must Be Reproducible.

All these things, I know, are obvious to some, and not obvious to others.

If you still have questions, ask. But don't argue. Because what I say is math. Unless you have another math (some people do), or another logic (there's plenty of them), please don't argue.

I'd be glad to see how all this changes if logic is e.g. linear. 

 

perdakot: (Default)

[personal profile] perdakot 2020-08-24 05:07 am (UTC)(link)
Диск и сеть туда же.

[personal profile] a2is 2020-08-24 08:43 am (UTC)(link)
Интерактивные тесты - туда же :)

[personal profile] mikkim08 2020-08-24 05:59 am (UTC)(link)
У меня было такое задание на интервью. Написать довольно простой вебсервис, который зависит от времени, и юнит-тест для него.

[personal profile] mikkim08 2020-08-24 09:09 am (UTC)(link)
Да нет, мне в целом интервью понравилось :)) Мне дали лаптоп, чтобы на нём программировать, интервьюер сидел рядом и очень доброжелательно помогал. Типа, pair-programming.

Сначала мы быстро-быстро написали простенький сервис (ипользуя jetty), а потом писали тесты, для чего нужно было рефакторить код. При этом пришлось выковыривать из бизнес-логики системный таймер и явно передавать его извне. А потом для тестов этот таймер "замочить" (от слова mock).

Ушло на это всё немногим больше часа.
dennisgorelik: 2020-06-13 in my home office (Default)

What to test in time-dependent function?

[personal profile] dennisgorelik 2020-08-24 06:01 pm (UTC)(link)
> потом писали тесты, для чего нужно было рефакторить код

Что именно вы тестировали, что требовало рефакторинг?

Nondeterministic function

[personal profile] dennisgorelik - 2020-08-24 23:34 (UTC) - Expand

Tests self-diagnostic

[personal profile] dennisgorelik - 2020-08-26 20:20 (UTC) - Expand

Re: Tests self-diagnostic

[personal profile] mikkim08 - 2020-08-27 20:11 (UTC) - Expand

Re: Tests self-diagnostic

[personal profile] dennisgorelik - 2020-08-27 23:35 (UTC) - Expand

Re: Tests self-diagnostic

[personal profile] dennisgorelik - 2020-08-28 00:19 (UTC) - Expand

Re: Tests self-diagnostic

[personal profile] dennisgorelik - 2020-08-28 08:48 (UTC) - Expand

Re: Tests self-diagnostic

[personal profile] mikkim08 - 2020-08-28 19:57 (UTC) - Expand

Re: Tests self-diagnostic

[personal profile] dennisgorelik - 2020-08-29 00:52 (UTC) - Expand

Re: Tests self-diagnostic

[personal profile] mikkim08 - 2020-08-29 20:33 (UTC) - Expand

Re: Tests self-diagnostic

[personal profile] dennisgorelik - 2020-08-29 21:52 (UTC) - Expand

Re: Tests self-diagnostic

[personal profile] mikkim08 - 2020-08-30 17:12 (UTC) - Expand

Re: Tests self-diagnostic

[personal profile] sassa_nf - 2020-08-29 08:12 (UTC) - Expand

Re: Tests self-diagnostic

[personal profile] mikkim08 - 2020-08-29 20:35 (UTC) - Expand
spamsink: (Default)

[personal profile] spamsink 2020-08-24 06:50 am (UTC)(link)
Or even 30 yeas from now.

Remember, remember the 19th of January 2038.

[personal profile] sassa_nf 2020-08-24 08:10 am (UTC)(link)
> A sequence of 100 numbers 4 for random.

I am not sure I understand what one could do about it. How do you defend against this? Write a piece of code for validating the randoms provided are sufficiently random?

It makes more sense to write tests supposing the entropy is depleted. (and the rng device blocks indefinitely)

[personal profile] sassa_nf 2020-08-24 04:46 pm (UTC)(link)
I think I get your point now. We shouldn't mock the entire RNG, but we do need to keep track what seed was used for a given test, so you can re-run the test, if it is seen to fail.

Re: blocking indefinitely - I was thinking of /dev/random. We can deplete it, and we have seen what happens to the programs that don't think that's possible.
norian: (Default)

[personal profile] norian 2020-08-24 08:28 am (UTC)(link)
рандомность в тестах часто обрабатывается записью какой-то достаточно длинной (для создания субъективной иллюзии) последовательности и канонического, прочеканого лапками, вывода, ну то есть регрешн

время есть область ответственности оси, прикладной софт не должно парить что где там переполнилось вообще
norian: (Default)

[personal profile] norian 2020-08-24 02:45 pm (UTC)(link)
это когда пишецца простой логгер, который вкрячиваецца в реальный источнег рандомной цыфири и потом его изображает, ну и конечно можно добавить туда чего угодно лапгами при желании
dennisgorelik: 2020-06-13 in my home office (Default)

How to test nondeterministic functions

[personal profile] dennisgorelik 2020-08-24 05:52 pm (UTC)(link)
> if you think you call a function in your code, and this function returns current time, or a random number... IT'S NOT A FUNCTION.

What you describe as "FUNCTION", is usually called "deterministic function".
~~~~~~~~~~
https://www.google.com/search?q=deterministic+function
Deterministic functions always return the same result any time they are called with a specific set of input values and given the same state of the database.
~~~~~~~~~~
Why did you chose using non-standard "function" label for "deterministic function" concept?

> So, if your code is written as something that retrieves this kind of data, to test your code, you should provide that data.

Not necessarily.
It depends on what exactly we are testing.
For example, if it is sufficient for us to test that nondeterministic function output contains specific phrase that does not depend on random input -- why should we provide random data to that test?

> Make sure that your tests don't depend on anything.

Test that does not depend on anything - does not test production code.
Do you use non-standard definition of word "anything" or phrase "don't depend"?

Edited 2020-08-24 17:54 (UTC)
dennisgorelik: 2020-06-13 in my home office (Default)

Re: How to test nondeterministic functions

[personal profile] dennisgorelik 2020-08-24 08:14 pm (UTC)(link)
What "idiots" are you referring to?

"Idiots" sign

[personal profile] dennisgorelik - 2020-08-24 23:24 (UTC) - Expand

Re: "Idiots" sign

[personal profile] dennisgorelik - 2020-08-24 23:49 (UTC) - Expand

Sings of understanding

[personal profile] dennisgorelik - 2020-08-25 15:24 (UTC) - Expand

Re: Sings of understanding

[personal profile] dennisgorelik - 2020-08-25 17:13 (UTC) - Expand

The book

[personal profile] dennisgorelik - 2020-08-25 17:38 (UTC) - Expand

Re: The book

[personal profile] dennisgorelik - 2020-08-25 18:47 (UTC) - Expand

Re: The book

[personal profile] dennisgorelik - 2020-08-25 19:17 (UTC) - Expand

Re: How to test nondeterministic functions

[personal profile] sassa_nf 2020-08-25 07:20 am (UTC)(link)
If you actually read what the search results are about, you would see it is about SQL specifically. It is no longer "usually". No one outside SQL community uses that term. Even the second best result - google corrected the term to "deterministic algorithm", because google knows people don't know what they are talking about, and what they really mean is algorithm.
dennisgorelik: 2020-06-13 in my home office (Default)

Is "nondeterministic function" a useful concept

[personal profile] dennisgorelik 2020-08-25 04:01 pm (UTC)(link)
> No one outside SQL community uses that term.

Your observation is incorrect.

https://www.google.com/search?q=%22deterministic+function%22
About 451,000 results

https://www.google.com/search?q=%22deterministic+function%22+-sql
About 524,000 results

Google's search count estimates are a bit weird here, but the main point is that there are plenty of "deterministic function" results with the word "SQL" in it.

> Even the second best result - google corrected the term to "deterministic algorithm"

It looks like you forgot to add quotes around "deterministic algorithm" in your search.

Misunderstanding question

[personal profile] dennisgorelik - 2020-08-26 14:48 (UTC) - Expand

Re: "то есть" trick

[personal profile] sassa_nf - 2020-08-26 15:45 (UTC) - Expand

Re: "то есть" trick

[personal profile] dennisgorelik - 2020-08-26 18:16 (UTC) - Expand
dennisgorelik: 2020-06-13 in my home office (Default)

Infallible teacher

[personal profile] dennisgorelik 2020-08-24 05:57 pm (UTC)(link)
> But don't argue.

Because you are perfect and cannot make mistakes, right?
dennisgorelik: 2020-06-13 in my home office (Default)

Re: Infallible teacher

[personal profile] dennisgorelik 2020-08-24 08:12 pm (UTC)(link)
> demonstrate your expertise

I demonstrated my expertise.
Unfortunately, today you are unwilling to comprehend it.

What kind of demonstration would convince you?
Edited 2020-08-24 20:15 (UTC)

Re: Infallible teacher

[personal profile] dennisgorelik - 2020-08-24 21:55 (UTC) - Expand

[identity profile] http://users.livejournal.com/sorcerer-/ 2020-08-28 09:27 am (UTC)(link)
Мне не очень понятно почему оно не happy path?
Мне очевидно, что любое конечное количество моков - это happy path при бесконечном оригинале.
Ну и так же, что все, что придумает конкретный программист, - это тоже happy path, т.к. он уже это придумал, и значит предусмотрел в коде.
Я считаю, что тесты бывают только двух видов: integration - что реально видит наш юзер и regression - настоящий un-happy path из баг репортов. Все остальное - карго культ.