forked from yuxh/gearman-go
		
	Fixed and improved Work() without Ready() test:
* FIX: committed test froze * FIX: committed test had a race condition! * Added properly handled panic test * Timeouts so that these tests should fail now if something goes wrong instead of failing.
This commit is contained in:
		
							parent
							
								
									6688c29c37
								
							
						
					
					
						commit
						0a4489d1fe
					
				@ -3,6 +3,7 @@ package worker
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var worker *Worker
 | 
					var worker *Worker
 | 
				
			||||||
@ -84,26 +85,85 @@ func TestWorkerClose(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func TestWorkWithoutReady(t * testing.T){
 | 
					func TestWorkWithoutReady(t * testing.T){
 | 
				
			||||||
	other_worker := New(Unlimited)
 | 
						other_worker := New(Unlimited)
 | 
				
			||||||
	var wg sync.WaitGroup
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err := other_worker.AddServer(Network, "127.0.0.1:4730"); err != nil {
 | 
						if err := other_worker.AddServer(Network, "127.0.0.1:4730"); err != nil {
 | 
				
			||||||
		t.Error(err)
 | 
							t.Error(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if err := other_worker.AddFunc("foobar", foobar, 0); err != nil {
 | 
						if err := other_worker.AddFunc("gearman-go-workertest", foobar, 0); err != nil {
 | 
				
			||||||
		t.Error(err)
 | 
							t.Error(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						timeout := make(chan bool, 1)
 | 
				
			||||||
 | 
						done := make( chan bool, 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	other_worker.JobHandler = func( j Job ) error {
 | 
						other_worker.JobHandler = func( j Job ) error {
 | 
				
			||||||
		if( ! other_worker.ready ){
 | 
							if( ! other_worker.ready ){
 | 
				
			||||||
			t.Error("Worker not ready as expected");
 | 
								t.Error("Worker not ready as expected");
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		wg.Done()
 | 
							done <-true
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						go func() {
 | 
				
			||||||
 | 
							time.Sleep(5 * time.Second)
 | 
				
			||||||
 | 
							timeout <- true
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	go other_worker.Work();
 | 
						go func(){
 | 
				
			||||||
 | 
							other_worker.Work();
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// With the all-in-one Work() we don't know if the 
 | 
				
			||||||
 | 
						// worker is ready at this stage so we may have to wait a sec:
 | 
				
			||||||
 | 
						go func(){
 | 
				
			||||||
 | 
							tries := 3
 | 
				
			||||||
 | 
							for( tries > 0 ){
 | 
				
			||||||
 | 
								if other_worker.ready {
 | 
				
			||||||
 | 
									other_worker.Echo([]byte("Hello"))
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// still waiting for it to be ready..
 | 
				
			||||||
 | 
								time.Sleep(1 * time.Second)
 | 
				
			||||||
 | 
								tries--
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						// determine if we've finished or timed out:
 | 
				
			||||||
 | 
						select{
 | 
				
			||||||
 | 
						case <- timeout:
 | 
				
			||||||
 | 
							t.Error("Test timed out waiting for the worker")
 | 
				
			||||||
 | 
						case <- done:
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestWorkWithoutReadyWithPanic(t * testing.T){
 | 
				
			||||||
 | 
						other_worker := New(Unlimited)
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						timeout := make(chan bool, 1)
 | 
				
			||||||
 | 
						done := make( chan bool, 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Going to work with no worker setup.
 | 
				
			||||||
 | 
						// when Work (hopefully) calls Ready it will get an error which should cause it to panic()
 | 
				
			||||||
 | 
						go func(){
 | 
				
			||||||
 | 
							defer func() {
 | 
				
			||||||
 | 
								if err := recover(); err != nil {
 | 
				
			||||||
 | 
									done <- true
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								t.Error("Work should raise a panic.")
 | 
				
			||||||
 | 
								done <- true
 | 
				
			||||||
 | 
							}()
 | 
				
			||||||
 | 
							other_worker.Work();
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
						go func() {
 | 
				
			||||||
 | 
							time.Sleep(2 * time.Second)
 | 
				
			||||||
 | 
							timeout <- true
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						select{
 | 
				
			||||||
 | 
						case <- timeout:
 | 
				
			||||||
 | 
							t.Error("Test timed out waiting for the worker")
 | 
				
			||||||
 | 
						case <- done:
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	wg.Add(1)
 | 
					 | 
				
			||||||
	worker.Echo([]byte("Hello"))
 | 
					 | 
				
			||||||
	wg.Wait();
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user