berkeleybross;11655 wrote:
I see what you're saying. On the off chance you have experience with this kind of thing, would you recommend any strategies for detecting orphans? I like the idea that I can "adopt" orphans from previous processes but how would you identify which ones are "in use"? Would keeping a log file of active/abandoned instances on disk, with a suitable timeout period before a forced clean up be a reliable method?
This depends a bit on how the processes are launched by your own code. The key piece of information would be the Process ID. If you can find a way to identify the process ID of the Solr instance being used by your test run, then you can store this somewhere in a globally accessible location (i.e. file system, memory mapped file, etc).
Basically you'd get the PID of the Solr instance, tie this up with the PID of your test runner process (Process.GetCurrentProcess()), then put these values together as an entry in your global state. At the start of every test run, the test goes and reads from this state and enumerates all entries, comparing these with the output from Process.GetProcesses(). Test processes that are listed in the global state but aren't showing up in Process.GetProcesses() must be from tests that have finished, and you can safely kill their related Solr process instances.
The hard part may well be getting the PID of the Solr process instance. If the API doesn't give you this, there is still a messy way to get it. You can infer the value from the timing of the Solr process being launched. For example:
1. Check the list of running processes, remember all the Solr ones
2. Initialise Solr process
3. (In a loop) Repeatedly query the list of running processes, comparing against the list from 1. until a new process shows up that isn't known
4. Take note of the PID of the new process
The above gets more complicated when running tests in parallel, because they can interfere with each other. So you'd need to wrap this up using System.Threading.Mutex to make sure only one test is launching Solr at any one time.
Of course, if Solr just has a .GetProcessId(), then it's much easier :)