By Ugorji Nwoke   14 Nov 2011   /blog   appengine geek golang technology

Testing Go App Engine Applications natively

With changes to allow concurrent requests in Go App Engine, Testing support follows naturally and natively.

Following support for concurrent requests described previously, Testing support is as easy as ensuring the following is called one time before your test is run. I have tested it and it works flawlessly.

(I call this once using sync.Once.Do(…) within init method)

 //don't conflict with http socket that devserver's go_app uses (switch ugorji with your username)
 os.Remove("unix:/tmp/dev_appserver_ugorji_8080_socket_http_2")
 flag.Set("addr_http", "unix:/tmp/dev_appserver_ugorji_8080_socket_http_2")
 flag.Set("addr_api", "unix:/tmp/dev_appserver_ugorji_8080_socket_api")
 go appengine_internal.Main()
 time.Sleep(1e9)

Once this is done, and you have a Python Dev Server running, then all your normal calls work.

To create a context and use it:

 req, _ := http.NewRequest("GET", "/", nil)
 req.Header.Set("X-AppEngine-Inbound-AppId", "dev~app")
 ctx := appengine.NewContext(req)

We need to do the dance of setting flags and stuff because the appengine_internal.Main is what is called by your app’s main() method. It uses parameters passed on the command line, and it internally will start a server socket for http (which is why we have to run it in a goroutine). We have to use this function because it is exported.

We really only need the initAPI function (which would be a 1-line call to make testing seamless).

appengine_internal.InitAPI("unix", "/tmp/dev_appserver_ugorji_8080_socket_api")

To make this easier, it would be nice if the appengine_internal.initAPI function is exported:

appengine_internal.InitAPI(netw, addr string)
Tags: appengine geek golang technology


Subscribe: Technology
© Ugorji Nwoke