Skip to content

Commit

Permalink
Merge pull request #67 from asandeep/app-engine-support
Browse files Browse the repository at this point in the history
Adds Google App Engine Support for neoism.
  • Loading branch information
jmcvetta committed Feb 18, 2015
2 parents 820c038 + cdfac06 commit 330a91a
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 43 deletions.
14 changes: 1 addition & 13 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@

package neoism

import (
"log"
"testing"
)

func connectBench(b *testing.B) *Database {
log.SetFlags(log.Ltime | log.Lshortfile)
db, err := Connect("http://localhost:7474/db/data")
if err != nil {
b.Fatal(err)
}
return db
}
import "testing"

func benchCleanup(b *testing.B, db *Database) {
qs := []*CypherQuery{
Expand Down
30 changes: 30 additions & 0 deletions benchmark_test_gae.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.

// +build appengine

package neoism

import (
"log"
"testing"

"appengine/aetest"
)

func connectBench(b *testing.B) *Database {
gaeContext, err := aetest.NewContext(nil)
if err != nil {
b.Fatal(err)
}
defer gaeContext.Close()

log.SetFlags(log.Ltime | log.Lshortfile)
db, err := Connect("http://localhost:7474/db/data", gaeContext)

if err != nil {
b.Fatal(err)
}
return db
}
21 changes: 21 additions & 0 deletions benchmark_test_standalone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.

// +build !appengine

package neoism

import (
"log"
"testing"
)

func connectBench(b *testing.B) *Database {
log.SetFlags(log.Ltime | log.Lshortfile)
db, err := Connect("http://localhost:7474/db/data")
if err != nil {
b.Fatal(err)
}
return db
}
34 changes: 34 additions & 0 deletions connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.

// +build !appengine

package neoism

import (
"net/http"
"net/url"

"github.com/jmcvetta/napping"
)

// Connect setups parameters for the Neo4j server
// and calls ConnectWithRetry()
func Connect(uri string) (*Database, error) {
h := http.Header{}
h.Add("User-Agent", "neoism")
db := &Database{
Session: &napping.Session{
Header: &h,
},
}
parsedUrl, err := url.Parse(uri)
if err != nil {
return nil, err
}
if parsedUrl.User != nil {
db.Session.Userinfo = parsedUrl.User
}
return connectWithRetry(db, parsedUrl, 0)
}
39 changes: 39 additions & 0 deletions connect_gae.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.

// +build appengine

package neoism

import (
"net/http"
"net/url"

"appengine"
"appengine/urlfetch"

"github.com/jmcvetta/napping"
)

// Modified version of Connect that support Google App Engine.
// Connect setups parameters for the Neo4j server
// and calls ConnectWithRetry()
func Connect(uri string, gaeContext appengine.Context) (*Database, error) {
h := http.Header{}
h.Add("User-Agent", "neoism")
db := &Database{
Session: &napping.Session{
Header: &h,
Client: urlfetch.Client(gaeContext),
},
}
parsedUrl, err := url.Parse(uri)
if err != nil {
return nil, err
}
if parsedUrl.User != nil {
db.Session.Userinfo = parsedUrl.User
}
return connectWithRetry(db, parsedUrl, 0)
}
24 changes: 2 additions & 22 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package neoism

import (
"errors"
"github.com/jmcvetta/napping"
"log"
"net/http"
"net/url"
"strconv"

"github.com/jmcvetta/napping"
)

// A Database is a REST client connected to a Neo4j database.
Expand All @@ -30,26 +30,6 @@ type Database struct {
Extensions interface{} `json:"extensions"`
}

// Connect setups parameters for the Neo4j server
// and calls ConnectWithRetry()
func Connect(uri string) (*Database, error) {
h := http.Header{}
h.Add("User-Agent", "neoism")
db := &Database{
Session: &napping.Session{
Header: &h,
},
}
parsedUrl, err := url.Parse(uri)
if err != nil {
return nil, err
}
if parsedUrl.User != nil {
db.Session.Userinfo = parsedUrl.User
}
return connectWithRetry(db, parsedUrl, 0)
}

// connectWithRetry tries to establish a connection to the Neo4j server.
// If the ping successes but doesn't return version,
// it retries using Path "/db/data/" with a max number of retries of 3.
Expand Down
20 changes: 12 additions & 8 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@ is not recommended to run this test suite on a db containing valuable data - run
it on a throwaway testing db instead! It's possible we could reduce this risk by
using defer() for cleanup.
To run these test for Google App Engine, run:
goapp test
*/

package neoism

import (
"github.com/bmizerany/assert"
"github.com/jmcvetta/randutil"
"log"
"os"
"testing"

"github.com/bmizerany/assert"
"github.com/jmcvetta/randutil"
)

func connectTest(t *testing.T) *Database {
log.SetFlags(log.Ltime | log.Lshortfile)
db, err := Connect("http://localhost:7474/db/data")
db, err := dbConnect("http://localhost:7474/db/data")
// db.Session.Log = true
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -77,29 +81,29 @@ func TestConnectInvalidUrl(t *testing.T) {
//
// Missing protocol scheme - url.Parse should fail
//
_, err := Connect("://foobar.com")
_, err := dbConnect("://foobar.com")
if err == nil {
t.Fatal("Expected error due to missing protocol scheme")
}
//
// Unsupported protocol scheme - Session.Get should fail
//
_, err = Connect("foo://bar.com")
_, err = dbConnect("foo://bar.com")
if err == nil {
t.Fatal("Expected error due to unsupported protocol scheme")
}
//
// Not Found
//
_, err = Connect("http://localhost:7474/db/datadatadata")
_, err = dbConnect("http://localhost:7474/db/datadatadata")
assert.Equal(t, InvalidDatabase, err)
}

func TestConnectIncompleteUrl(t *testing.T) {
//
// 200 Success and HTML returned
//
_, err := Connect("http://localhost:7474")
_, err := dbConnect("http://localhost:7474")
if err != nil {
t.Fatal("Hardsetting path on incomplete url failed")
}
Expand Down Expand Up @@ -157,7 +161,7 @@ func TestPropertyKeys(t *testing.T) {

func TestConnectUrl(t *testing.T) {
if url := os.Getenv("NEO4J_URL"); url != "" {
_, err := Connect(url)
_, err := dbConnect(url)
if err != nil {
t.Fatal("Cannot connect to ", url, err)
}
Expand Down
18 changes: 18 additions & 0 deletions database_test_gae.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.

// +build appengine

package neoism

import "appengine/aetest"

func dbConnect(url string) (*Database, error) {
gaeContext, err := aetest.NewContext(nil)
if err != nil {
return nil, err
}

return Connect(url, gaeContext)
}
11 changes: 11 additions & 0 deletions database_test_standalone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.

// +build !appengine

package neoism

func dbConnect(url string) (*Database, error) {
return Connect(url)
}

0 comments on commit 330a91a

Please sign in to comment.