func TestLocationAgent(t *testing.T) { db := mem.NewDB() u, _, err := user.Create(db, "username", "password") if err != nil { t.Fatal(err) } changes := data.FilterKind(db.Changes(), models.ProfileKind) ctx, stop := context.WithCancel(context.Background()) go agents.LocationAgent(ctx, db, u) defer stop() // give control to agent thread time.Sleep(1 * time.Millisecond) _, loc, err := event.LocationUpdate(db, u, 50, 50, 50) if err != nil { t.Fatal(err) } select { case profileChange := <-*changes: p := profileChange.Record.(*models.Profile) if loc.Id != p.LocationId { t.Fatal("Expected profile's location id to now match be the new location ") } case <-time.After(100 * time.Millisecond): t.Fatal("Timed out waiting for profile update") } }
func webSensorLocation(db data.DB, u *models.User, eventData map[string]interface{}) { log.Printf("%++v", eventData) webTag, err := tag.ForName(db, u, "WEB") if err != nil { log.Fatal(err) } lat, ok := eventData["latitude"].(float64) if !ok { return // bail } lon, ok := eventData["longitude"].(float64) if !ok { return // bail } _, _, err = event.LocationUpdate( db, u, 0, lat, lon, webTag, ) if err != nil { log.Fatal(err) } }
// currently blocks forever? func TestLocationAgent(t *testing.T) { t.Skip() db, _, s := testInstance(t, context.Background()) defer s.Close() u, cred := testUser(t, db) t.Log("Opening websocket") serverURL := s.URL origin := serverURL wsURL := strings.Replace(serverURL, "http", "ws", 1) params := url.Values{} params.Set("public", cred.Public) params.Set("private", cred.Private) params.Set("kind", models.ProfileKind.String()) wsURL += routes.RecordChanges + "?" + params.Encode() ws, err := websocket.Dial(wsURL, "", origin) if err != nil { t.Fatal(err) } defer ws.Close() t.Log("Websocket openened") t.Log("Creating new location update") var loc *models.Location if _, loc, err = event.LocationUpdate(db, u, 50, 50, 50); err != nil { t.Fatal(err) } t.Log("Created location update") time.Sleep(500 * time.Millisecond) log.Print("h") var ct transfer.ChangeTransport if err := websocket.JSON.Receive(ws, &ct); err != nil { t.Fatal(err) } log.Print("h") tc := transfer.ChangeFrom(&ct, models.ModelFor(ct.RecordKind)) if tc.ChangeKind != data.Update { t.Fatal("Expected ChangeKind to be Update") } if tc.Record.(*models.Profile).LocationId != loc.Id { t.Fatal("Expected profile to have new location's id") } }