-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[10.x] Siesta #46904
[10.x] Siesta #46904
Conversation
Thoughts on Rest for the name? |
I think there is a typo in the last example regarding float and rounding, it says that Siesta sleeps for 90 seconds when passing 1.5? 👀 or am I misunderstanding something. But I’m loving this idea!
|
@LasseRafn corrected. thank you! Time is hard. Math is hard. Math + Time = 🫠 |
Would personally prefer a name like Sleep, Wait or Rest. Otherwise love this! |
Rest might add confusion because of the REST. |
Love it! Would humbly submit |
@fourstacks I actually used Snooze when I first conceptualised this - but realised it conflicted with https://github.com/thomasjohnkane/snooze |
I absolutely love this 😆 I have a few tests for my own that last 5-10 seconds because of a sleep, would be cool to fake it! |
I also agree with @fourstacks I do like Or even being boring and having |
Done this a few times by just creating |
11th hour realisation which I can’t implement right now: we need… Siesta::assertSlept(Siesta::for(1)->second()); that doesn’t care about order. |
@@ -114,7 +115,7 @@ public function block($seconds, $callback = null) | |||
$starting = $this->currentTime(); | |||
|
|||
while (! $this->acquire()) { | |||
usleep($this->sleepMilliseconds * 1000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the complexity of the Siest
class, I would rather not introduce it into the existing src
code, as is very risky and it could potentially disrupt the current behavior, even if it appears otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think at a bare minimum we want it used in the retry
helper. After that, in order of what I would want to optionally see:
- Timebox
- Cache stuff
I actually like |
Co-authored-by: Nuno Maduro <enunomaduro@gmail.com>
Co-authored-by: Nuno Maduro <enunomaduro@gmail.com>
Draft as there are some final tweaks I wanna make. |
I prefer just a regular I don't think I'll ever need this class myself though since I can't think of many practical usecases where I need complex sleep behaviour like in this Siesta class. But for internal/testing usecases it might be handy. |
Definitely has potential use cases for third-party integrations and |
@timacdonald There are quite a number of people who prefer “sleep” over “siesta”. Have you considered the name change? It’s a great name but I understand people’s feelings against it. Edit: Perhaps a poll in the PR or maybe a Twitter poll? |
@Sammyjo20 naming considerations and alternatives are part of the description. I lowkey like "Siesta". If Taylor decides this feature is suitable for the framework and wants a name change, I'm happy to change it. No need for polls. |
I'd suggest the assertSleptTimes to be assertTimesSlept. Otherwise, love it. |
If we are keen for this feature, but are not keen on the Won't implement it until we are sure we want the feature in the framework. |
src/Illuminate/Support/Siesta.php
Outdated
if ($seconds > 0) { | ||
sleep($seconds); | ||
|
||
$remaining = $remaining->subSeconds($seconds); | ||
} | ||
|
||
$microseconds = (int) $remaining->totalMicroseconds; | ||
|
||
if ($microseconds > 0) { | ||
usleep($microseconds); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified imo. In one of my projects I have added a helper that combines sleep()
and usleep()
so i can do fractional sleep easily like so:
function my_sleep($microseconds)
{
if ($microseconds <= 0) {
return;
}
if ($usleep = $microseconds % 1000000) {
usleep($usleep);
}
if ($sleep = ($microseconds - $usleep) / 1000000) {
sleep($sleep);
}
}
Then you can call that method (maybe even expose it as public/static since it is very handy) and be done with it. The CarbonInterval $duration
property in your class is probably too complex since you can just use a microseconds integer instead. We are mostly talking about times in that neighborhood anyway (maybe up to a few seconds).
Copying this over from Twitter. My bad for not keeping it in the PR.🙏 Context: https://twitter.com/timacdonald87/status/1653151918863302656?s=46&t=zY_3ch9pBvnnjyXjsgTbnQ I see where you are coming from, @timacdonald I still believe it’s possible to use it wrong. Actually, imho, there should/must be some enforcement of calling Let’s say I do something ‚weird‘ like: $siesta = Siesta::for(2)->minutes();
if($bigWhateverAmountHereOne) {
// and(2)
}
if($bigWhateverAmountHereTwo) {
// and(5)
}
$siesta->minutes() Theoretically, because both There should be a check in Disclaimer: on my phone and not able to actually playing around with it. So it’s all theoretical. 🙏 Appreciate your work, Tim! ❤️ |
@NickSdot, I think, there is no need on exceptions. Just add every add() to accumulator should be ok. Siesta class (or Sleep if smb wish), shouldn't be responsible on watching for errors in your code. |
@Angel5a at first I was thinking that too, as my initial note on it shows. But it allows to accumulate time without defining which unit the time has. Siesta class (or Sleep if smb wish), shouldn't be responsible on watching for errors in your code. Whether or not the class should be responsible to allow or disallow me to write wrong code is a good question -- I tend to the latter, but also understand why you see it different. Tim and team will have their take on it, I guess. |
Renamed to |
Appreciate all the feedback from everyone ❤️. I hope the feature comes in handy. Also: On the day of release I'll be all like... use Illuminate\Support\Sleep as Siesta; 😎 |
commit 025c912 Author: Taylor Otwell <taylor@laravel.com> Date: Fri May 5 14:47:22 2023 -0500 respect parents on middleware priority (laravel#46972) commit 07a5e09 Author: Saya <379924+chu121su12@users.noreply.github.com> Date: Sat May 6 01:36:04 2023 +0800 [10.x] Add url support for mail config (laravel#46964) * Add url support for mail config * ci fix * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> commit 1e6b467 Author: Tim MacDonald <hello@timacdonald.me> Date: Sat May 6 03:00:49 2023 +1000 wip (laravel#46963) commit 1c783fe Author: Günther Debrauwer <gunther@nextapps.be> Date: Thu May 4 17:27:41 2023 +0200 [10.x] Add 'hashed' cast (laravel#46947) * Add 'hashed' cast * Fix linting issues commit 51251d4 Author: taylorotwell <taylorotwell@users.noreply.github.com> Date: Thu May 4 14:54:58 2023 +0000 Update facade docblocks commit 82a8da7 Author: Anjorin Damilare <damilareanjorin1@gmail.com> Date: Thu May 4 15:54:23 2023 +0100 [10.x] add expression to DB table doctype (laravel#46955) commit 65f6426 Author: Wouter de Jong <wouterj@users.noreply.github.com> Date: Thu May 4 14:41:20 2023 +0200 Fix typo in PHPdoc tag (laravel#46960) commit c164078 Author: Tim MacDonald <hello@timacdonald.me> Date: Thu May 4 05:17:54 2023 +1000 [10.x] Siesta (laravel#46904) * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Update src/Illuminate/Support/Siesta.php Co-authored-by: Nuno Maduro <enunomaduro@gmail.com> * Update src/Illuminate/Support/Siesta.php Co-authored-by: Nuno Maduro <enunomaduro@gmail.com> * Update src/Illuminate/Support/Siesta.php Co-authored-by: Nuno Maduro <enunomaduro@gmail.com> * wip * wip * wip * wip * wip * wip * wip * wip * formatting and rename to sleep --------- Co-authored-by: Nuno Maduro <enunomaduro@gmail.com> Co-authored-by: Taylor Otwell <taylor@laravel.com> commit 6e8b883 Author: Volodya Kurshudyan <70023120+xurshudyan@users.noreply.github.com> Date: Wed May 3 22:19:29 2023 +0400 Add sortRecursiveDesc() method (laravel#46945) Co-authored-by: Volodya Khurshudyan <volodya.khurshudyan@softconstruct.com> commit d800f9e Author: Abu Sayed Jobayer <aszubayr@gmail.com> Date: Wed May 3 19:51:21 2023 +0600 Update doc block to make array notation more consist. (laravel#46942) commit ba46acb Author: Bert <bertwijnhoven@hotmail.com> Date: Tue May 2 23:24:37 2023 +0200 [10.x] Expose `Js::json()` helper (laravel#46935) * change to static * Update Js.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> commit 2aff286 Author: StyleCI Bot <bot@styleci.io> Date: Mon May 1 16:09:32 2023 +0000 Apply fixes from StyleCI commit f8dd01a Author: Italo <DarkGhostHunter@Gmail.com> Date: Mon May 1 12:09:09 2023 -0400 [10.x] Adds ability to restore/set Global Scopes (laravel#46922) * [10.x] Adds ability to restore/set Global Scopes * Update HasGlobalScopes.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> commit a2f35fa Author: Günther Debrauwer <gunther@nextapps.be> Date: Mon May 1 18:07:12 2023 +0200 [10.x] Use method on UploadedFile to validate image dimensions (laravel#46912) * imageSize method on uploadedfile * formatting * fix variable --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> commit 8de1aa2 Author: Kieran <kieran@supportpal.com> Date: Mon May 1 17:05:23 2023 +0100 [10.x] Mark commands as isolatable (laravel#46925) * Ability to set default for --isolated option * set default exit code * Update Command.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> commit 812ef55 Author: Christian Rishøj <christian@rishoj.net> Date: Mon May 1 17:52:24 2023 +0200 follow default PHP behavior and replace invalid codepoints (laravel#46914) commit 96f0d0e Author: Choraimy Kroonstuiver <3661474+axlon@users.noreply.github.com> Date: Mon May 1 17:41:14 2023 +0200 Add missing typehint to cache repository contract (laravel#46929) commit 89ac58a Author: Taylor Otwell <taylor@laravel.com> Date: Thu Apr 27 10:29:15 2023 -0500 fix replace missing_unless commit 5f30445 Merge: 9cd734c f43355f Author: Tetiana Blindaruk <t.blindaruk@gmail.com> Date: Tue Apr 25 20:33:53 2023 +0300 Merge remote-tracking branch 'origin/10.x' into 10.x # Conflicts: # CHANGELOG.md commit 9cd734c Author: Tetiana Blindaruk <t.blindaruk@gmail.com> Date: Tue Apr 25 20:33:29 2023 +0300 [10.x] Update CHANGELOG.md commit f43355f Author: TBlindaruk <TBlindaruk@users.noreply.github.com> Date: Tue Apr 25 16:47:37 2023 +0000 Update CHANGELOG commit 3507812 Merge: e2a65b3 675ea86 Author: Taylor Otwell <taylor@laravel.com> Date: Tue Apr 25 08:47:18 2023 -0500 fix conflicts commit 675ea86 Author: Taylor Otwell <taylor@laravel.com> Date: Tue Apr 25 08:44:05 2023 -0500 version commit e2a65b3 Author: Bogdan Lotarev <bogdanlotar@gmail.com> Date: Tue Apr 25 15:40:49 2023 +0200 [10.x] Use foreignUlid if model uses HasUlids trait when call foreignIdFor (laravel#46876) * fix: use foreignUlid if model uses HasUlids * test: add assert for MySql * Update Blueprint.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> commit b5bef6b Author: StyleCI Bot <bot@styleci.io> Date: Mon Apr 24 19:32:27 2023 +0000 Apply fixes from StyleCI commit 846d1a0 Author: Anjorin Damilare <damilareanjorin1@gmail.com> Date: Mon Apr 24 20:32:06 2023 +0100 [10.x]: improve job release method to accept date instance (laravel#46854) * [10.x]: improve job release method to accept date instance * Update InteractsWithQueue.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> commit 7136338 Author: Tim MacDonald <hello@timacdonald.me> Date: Tue Apr 25 05:16:49 2023 +1000 [10.x] Named static methods for middleware (laravel#46362) * wip * Standardise of `using` for Authorization middleware * Update ValidateSignature.php * Update ThrottleRequests.php * Update ThrottleRequests.php * Update RequirePassword.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> commit 2922575 Author: Nuno Maduro <enunomaduro@gmail.com> Date: Mon Apr 24 19:49:40 2023 +0100 [10.x] Uses `@template-covariant` in collections (laravel#46872) * docs: update collection docs to use template-covariant for the values This allows developers to use classes with inheritance on collections. See types/Support/Collection.php for an example * Revert `@template-covariant` on Arrayable * Apply fixes from StyleCI --------- Co-authored-by: rvanvelzen <rvanvelzen@swis.nl> Co-authored-by: StyleCI Bot <bot@styleci.io> commit 1793066 Author: Finn <71390226+FinnPaes@users.noreply.github.com> Date: Mon Apr 24 17:59:54 2023 +0200 Fix implode docblock to accept callable as parameter (laravel#46869) Co-authored-by: Finn Paes <finn@justbetter.nl> commit 89a5468 Author: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Mon Apr 24 09:43:14 2023 -0400 [10.x] Throw LogicException when calling `FileFactory@image()` if mimetype is not supported (laravel#46859) * throw exception if FileFactory does not support mimetype * style * formatting * rely on $functionName --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> commit 9cfd7e1 Author: Niclas <NiclasvanEyk@users.noreply.github.com> Date: Mon Apr 24 15:25:43 2023 +0200 Pass through IGNITION_LOCAL_SITES_PATH environment variable when serving (laravel#46857) Enables correct link generation for opening files from the error page when running inside docker. commit d199af3 Author: s4muel <s4muel@users.noreply.github.com> Date: Fri Apr 21 17:14:58 2023 +0200 [10.x] update return type in docblock for Process pipe method (laravel#46848) * update return type in docblock for Process pipe method * update the Process.php docblock as well * Update Factory.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> commit 0c9b3e9 Author: Dries Vints <dries@vints.io> Date: Fri Apr 21 15:14:53 2023 +0200 Make rules method in FormRequest optional (laravel#46846) commit 3d28bdc Author: James Hulse <jameshulse@users.noreply.github.com> Date: Thu Apr 20 18:37:59 2023 +0100 [10.x] Allow pruning all cancelled and unfinished queue batches (laravel#46833) * Allow pruning all cancelled and unfinished batches * Apply fixes from StyleCI commit 2b463dd Author: Miran AL Mehrab <miranalmehrab@protonmail.com> Date: Thu Apr 20 22:29:37 2023 +0600 Add new HTTP status assertions (laravel#46841) * Adds gone status check * Adds service unavailable status check * Adds internal server error status check commit a3cfd2d Author: Tsuguya Toma <tsuguya.toma@tgy.io> Date: Wed Apr 19 22:54:02 2023 +0900 fix date_format rule throw ValueError (laravel#46824) commit ed75852 Author: Rudie Dirkx <168024+rudiedirkx@users.noreply.github.com> Date: Tue Apr 18 21:06:23 2023 +0200 Use pivot model fromDateTime instead of assuming Carbon (laravel#46822) commit 6f575fc Merge: 3259360 95ffddc Author: Tetiana Blindaruk <t.blindaruk@gmail.com> Date: Tue Apr 18 21:54:43 2023 +0300 Merge remote-tracking branch 'origin/10.x' into 10.x commit 3259360 Author: Tetiana Blindaruk <t.blindaruk@gmail.com> Date: Tue Apr 18 21:54:23 2023 +0300 [10.x] Update CHANGELOG.md commit 95ffddc Author: masoud derakhshi <59544612+mderakhshi@users.noreply.github.com> Date: Tue Apr 18 22:00:04 2023 +0330 [10.x] whereMorphedTo null (laravel#46821) * Update QueriesRelationships.php * Update DatabaseEloquentBuilderTest.php * Update DatabaseEloquentBuilderTest.php * Update DatabaseEloquentBuilderTest.php commit 7aab67d Author: Gaitholabi <24876890+Gaitholabi@users.noreply.github.com> Date: Tue Apr 18 21:18:06 2023 +0300 [10.x] Allow separate directory for locks on filestore (laravel#46811) * allow separate directory for locks on filestore * fix style * fix method signature * Update src/Illuminate/Cache/FileStore.php Co-authored-by: Dries Vints <dries@vints.be> * apply styleci * formatting --------- Co-authored-by: Dries Vints <dries@vints.be> Co-authored-by: Taylor Otwell <taylor@laravel.com> commit e838b1d Author: TBlindaruk <TBlindaruk@users.noreply.github.com> Date: Tue Apr 18 17:52:33 2023 +0000 Update CHANGELOG commit 9747b8c Author: Tetiana Blindaruk <t.blindaruk@gmail.com> Date: Tue Apr 18 20:48:03 2023 +0300 [9.x] Update CHANGELOG.md commit 5d8416e Merge: 317d7cc 16454f1 Author: Dries Vints <dries@vints.be> Date: Tue Apr 18 17:01:19 2023 +0200 Merge branch '9.x' into 10.x # Conflicts: # src/Illuminate/Foundation/Application.php commit 317d7cc Author: Taylor Otwell <taylor@laravel.com> Date: Tue Apr 18 08:45:33 2023 -0500 version commit 16454f1 Author: Taylor Otwell <taylor@laravel.com> Date: Tue Apr 18 08:44:55 2023 -0500 version commit 8ef7a8d Merge: a56d748 2a6713f Author: Taylor Otwell <taylor@laravel.com> Date: Tue Apr 18 08:44:34 2023 -0500 Merge branch '9.x' into 10.x commit 2a6713f Author: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Tue Apr 18 09:42:14 2023 -0400 [9.x] Release lock for job implementing `ShouldBeUnique` that is dispatched `afterResponse()` (laravel#46806) * failing test * rely on PendingDispatch@afterResponse so that ShouldBeUnique is checked * modifications to failing test * move lock/middleware handling to CallQueuedHandler@handle() * use CallQueuedHandler@handle if job employs InteractsWithQueue * style * revert changes to CallQueuedHandler & PendingDispatch * switch Bus\Dispatcher@dispatchAfterResponse to rely on Dispatcher@dispatchSync() * add `dispatchAfterResponse` test commit a56d748 Author: Mohd Hafizuddin M Marzuki <hafizuddin_83@yahoo.com> Date: Tue Apr 18 01:41:32 2023 +0800 Fix `validateDecimal()` (laravel#46809) commit 88c28e2 Author: Anjorin Damilare <damilareanjorin1@gmail.com> Date: Mon Apr 17 17:13:04 2023 +0100 [10.x]: add max exceptions to broadcast event (laravel#46800) commit 17ad285 Author: kazunari.ueeda <34956483+UserKazun@users.noreply.github.com> Date: Tue Apr 18 01:01:53 2023 +0900 Fix return value to int or null. (laravel#46802) commit 997218b Author: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Sun Apr 16 09:35:36 2023 -0400 fixes test timestamp properties (laravel#46795) commit 3e5a6b3 Author: teamradhq <14055161+teamradhq@users.noreply.github.com> Date: Sun Apr 16 07:55:40 2023 +1000 Fix deprecated class in request.stub (laravel#46787) * Replace deprecated Illuminate\Contracts\Validation\Rule with ValidationRule.` commit 0720d08 Author: Taylor Otwell <taylor@laravel.com> Date: Sat Apr 15 16:53:23 2023 -0500 [10.x] Minor skeleton slimming (framework edition) (laravel#46786) * allow web and api named args on routes method * add app skeleton broadcast provider in core * add default provider collection * remove base broadcast service provider * Apply fixes from StyleCI * revert route provider change --------- Co-authored-by: StyleCI Bot <bot@styleci.io> commit d95f865 Author: Sobhan <44964722+sobhan-m94@users.noreply.github.com> Date: Fri Apr 14 17:31:33 2023 +0330 Add headers (laravel#46780) * add headers * Update Application.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
* wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Update src/Illuminate/Support/Siesta.php Co-authored-by: Nuno Maduro <enunomaduro@gmail.com> * Update src/Illuminate/Support/Siesta.php Co-authored-by: Nuno Maduro <enunomaduro@gmail.com> * Update src/Illuminate/Support/Siesta.php Co-authored-by: Nuno Maduro <enunomaduro@gmail.com> * wip * wip * wip * wip * wip * wip * wip * wip * formatting and rename to sleep --------- Co-authored-by: Nuno Maduro <enunomaduro@gmail.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
This PR proposes a new utility class that wraps up the native
sleep
andusleep
PHP functions into a "fakeable" and testable class.The motivation behind this PR is to improve testing of sleeping and while also removing the duration of the sleep from the test itself while maintaining confidence in the test.
Often when an application interacts with a 3rd party library it will need to retry interactions after pausing execution.
An example of this may be a HTTP endpoint. The API may be down for a brief moment. Instead of failing, the application may use a "retry":
When attempting to test the failure / retry logic, a test would take a minimum of 1.5 seconds.
More generally, when using the
retry
helper, this is also a (minor) annoyance.To address this, we now use the
Siesta
helper in a few places throughout the Framework. The helper may the be "faked" in tests and some basic assertions may also be made.API
Fluent API
Composed durations
Siesta until a given time
1:1 replacement API
Testing
As mentioned, the motivation here is to improve testing. If an application simply wants tests to not "sleep", then the following could be added to any test that uses
Siesta
:It is also possible to make assertions:
Now any call to sleep via Siesta will not actually pause execution throughout the testsuite.
Naming alternatives
If the framework doesn't jive with "Siesta":
Sleep
Pause
Rest
…but lowkey “Siesta” is the best option 🙉
Use of Siesta within the Framework
I feel that the places I've used Siesta throughout the framework make sense. If we feel any of them don't make sense, I'm happy to remove any.
📌 Notes
The native
time_sleep_until
function will throw an error if the time has already passed; we just do not sleep. When testing, you would need to assert that it slept for zero seconds.The native
sleep
function will cast afloat
to anint
. Our implementation does not do this:The native
usleep
function may not support values larger than1000000
. Our implementation fixes this by using a combination ofsleep
andusleep
.Although PHP does have the ability to sleep for nanoseconds, via the native time_nanosleep function, I didn't feel the need to add an affordance to this.
An implementation would be possible, however I'm not sure it is worth it. If you need something that fine-grained, even our PHP code in-between things is probably going to be problematic.