Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #3358 > unrolled thread
| Started by | Simon <bad@example.com> |
|---|---|
| First post | 2011-10-10 10:16 +0200 |
| Last post | 2011-10-10 13:42 -0400 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.php
Test race condition? (unit test?) Simon <bad@example.com> - 2011-10-10 10:16 +0200
Re: Test race condition? (unit test?) Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 08:36 -0400
Re: Test race condition? (unit test?) Simon <bad@example.com> - 2011-10-10 15:48 +0200
Re: Test race condition? (unit test?) The Natural Philosopher <tnp@invalid.invalid> - 2011-10-10 15:43 +0100
Re: Test race condition? (unit test?) Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 13:42 -0400
| From | Simon <bad@example.com> |
|---|---|
| Date | 2011-10-10 10:16 +0200 |
| Subject | Test race condition? (unit test?) |
| Message-ID | <9ffnu3Fpb9U1@mid.individual.net> |
Hi, I have written a very simple file based lock/unlock system. the way it works is as follows. process A looks for File 'X', if file 'X' does not exists it tries to creates an empty file, 'Xa', and locks it, it then creates file 'X' and deletes file 'Xa'. process B is also looking for File 'X', if file 'X' does not exists it tries to create 'Xa', on failure it waits for a seconds and tries to obtain the lock again. Once/if the lock is obtained, it looks for file 'X' again and either uses it or creates it. process C looks for file 'X', if file 'X' exists it will use it. I try to write as much unit tests as possible and, as this is an important process, I would like to test the process above. 1- Can you think of any way I could test the above? As far as I know you cannot start multiple threads in php, (even in a dev environment). 2- Is there a better way of getting a lock like I am doing above? Many thanks Simon
[toc] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2011-10-10 08:36 -0400 |
| Message-ID | <j6uos6$kh0$1@dont-email.me> |
| In reply to | #3358 |
On 10/10/2011 4:16 AM, Simon wrote: > Hi, > > I have written a very simple file based lock/unlock system. > the way it works is as follows. > > process A looks for File 'X', if file 'X' does not exists it tries to > creates an empty file, 'Xa', and locks it, it then creates file 'X' and > deletes file 'Xa'. > > process B is also looking for File 'X', if file 'X' does not exists it > tries to create 'Xa', on failure it waits for a seconds and tries to > obtain the lock again. Once/if the lock is obtained, it looks for file > 'X' again and either uses it or creates it. > > process C looks for file 'X', if file 'X' exists it will use it. > > I try to write as much unit tests as possible and, as this is an > important process, I would like to test the process above. > > 1- Can you think of any way I could test the above? As far as I know you > cannot start multiple threads in php, (even in a dev environment). > > 2- Is there a better way of getting a lock like I am doing above? > > Many thanks > > Simon Race conditions are always difficult to duplicate and troubleshoot when they occur. And although with some third party tools you could process php scripts in different threads, you probably don't want to (locking between threads is different than locking between processes). The way to typically troubleshoot such problems is to run multiple copies of the script/program with waits for input at critical places. Run the script concurrently from multiple windows to emulate the system. The real question here is - what is the problem you're trying to resolve. Without knowing that, it's difficult to recommend if there is a better way. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ==================
[toc] | [prev] | [next] | [standalone]
| From | Simon <bad@example.com> |
|---|---|
| Date | 2011-10-10 15:48 +0200 |
| Message-ID | <9fgbe9Fu9aU1@mid.individual.net> |
| In reply to | #3363 |
> > The real question here is - what is the problem you're trying to > resolve. Without knowing that, it's difficult to recommend if there is a > better way. > Thanks for the reply. I am not trying to solve a problem per say. I am 'just' trying to prevent duplicate work. When a request for a certain page first comes, it make a few calls to the db and then caches the page. Once the page is cached, only one call needs to be made. But I sometime get hundreds of calls for a certain page, all at the same time. So it would make no sense for all of them to create the very same cache page. It makes more sense for the first request to cache the page wile others wait a few milliseconds for it. By locking the page, the first requests is instructing others that the work is already been done and they should wait a few milliseconds. Every so often, (for various reasons), the cache is marked as 'dirty' and a new cache will then be recreated. If one request 'locks' the page, and a cache is available, then the other requests will continue to serve the dirty page. I just want to prevent the same cache been created by many requests at the same time. I hope this makes sense. Simon
[toc] | [prev] | [next] | [standalone]
| From | The Natural Philosopher <tnp@invalid.invalid> |
|---|---|
| Date | 2011-10-10 15:43 +0100 |
| Message-ID | <j6v0ak$78d$1@news.albasani.net> |
| In reply to | #3364 |
Simon wrote: >> >> The real question here is - what is the problem you're trying to >> resolve. Without knowing that, it's difficult to recommend if there is a >> better way. >> > > Thanks for the reply. I am not trying to solve a problem per say. > I am 'just' trying to prevent duplicate work. > > When a request for a certain page first comes, it make a few calls to > the db and then caches the page. > Once the page is cached, only one call needs to be made. > > But I sometime get hundreds of calls for a certain page, all at the same > time. > So it would make no sense for all of them to create the very same cache > page. > > It makes more sense for the first request to cache the page wile others > wait a few milliseconds for it. > By locking the page, the first requests is instructing others that the > work is already been done and they should wait a few milliseconds. > > Every so often, (for various reasons), the cache is marked as 'dirty' > and a new cache will then be recreated. > If one request 'locks' the page, and a cache is available, then the > other requests will continue to serve the dirty page. > > I just want to prevent the same cache been created by many requests at > the same time. > well if you are using a database, it will already have cached the data comprising the page. Unless your manipulation of that page is excessively memory or CPU intensive, there is little point in caching the result too. And that is probably something a proxy server does better, anyway. > I hope this makes sense. > > Simon
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2011-10-10 13:42 -0400 |
| Message-ID | <j6vapm$l9k$1@dont-email.me> |
| In reply to | #3364 |
On 10/10/2011 9:48 AM, Simon wrote: >> >> The real question here is - what is the problem you're trying to >> resolve. Without knowing that, it's difficult to recommend if there is a >> better way. >> > > Thanks for the reply. I am not trying to solve a problem per say. > I am 'just' trying to prevent duplicate work. > > When a request for a certain page first comes, it make a few calls to > the db and then caches the page. > Once the page is cached, only one call needs to be made. > > But I sometime get hundreds of calls for a certain page, all at the same > time. > So it would make no sense for all of them to create the very same cache > page. > > It makes more sense for the first request to cache the page wile others > wait a few milliseconds for it. > By locking the page, the first requests is instructing others that the > work is already been done and they should wait a few milliseconds. > > Every so often, (for various reasons), the cache is marked as 'dirty' > and a new cache will then be recreated. > If one request 'locks' the page, and a cache is available, then the > other requests will continue to serve the dirty page. > > I just want to prevent the same cache been created by many requests at > the same time. > > I hope this makes sense. > > Simon Are you sure you need this? Databases will cache the results and future db requests for the same information are generally quite fast. Are you having performance problems? And if so, have you looked at where your performance problems are? And if so, have you tried to optimize your code? Depending on what's going on, trying to cache pages can actually add overhead to the server instead of relieving it. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ==================
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web