Exemple #1
0
// 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")
	}
}
Exemple #2
0
func TestRecordChanges(t *testing.T) {
	ctx, cancelAllConnections := context.WithCancel(context.Background())
	defer cancelAllConnections()

	db, _, s := testInstance(t, ctx)
	defer s.Close()

	user, cred := testUser(t, db)

	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.TaskKind.String())
	wsURL += routes.RecordChanges + "?" + params.Encode()
	t.Logf("Constructed URL: %s", wsURL)

	t.Log("Opening websocket")
	ws, err := websocket.Dial(wsURL, "", origin)
	if err != nil {
		t.Fatal(err)
	}
	defer ws.Close()
	t.Log("Websocket openened")

	time.Sleep(500 * time.Millisecond)

	t.Log("Creating task")
	taskName := "task to retreive"
	task := models.NewTask()
	task.SetID(db.NewID())
	task.CreatedAt = time.Now()
	task.OwnerId = user.Id
	task.Name = taskName
	task.UpdatedAt = time.Now()
	if err := db.Save(task); err != nil {
		t.Fatal(err)
	}
	t.Log("Task created")

	var ct transfer.ChangeTransport
	if err := websocket.JSON.Receive(ws, &ct); err != nil {
		t.Fatal(err)
	}

	tc := transfer.ChangeFrom(&ct, models.ModelFor(ct.RecordKind))

	t.Logf("Task Change Recieved: %++v", tc)

	t.Log("Cancelling the connection")
	cancelAllConnections()
	t.Log("\tcancelled")

	if tc.ChangeKind != data.Update {
		t.Fatal("Expected ChangeKind to be Update")
	}

	if tc.Record.(*models.Task).Name != taskName {
		t.Fatalf("Expected task name to be: '%s'", taskName)
	}
}