Example #1
0
func TestChunkUpdate(t *testing.T) {
	tick := message.Tick(42)
	blk := message.NewBlock()
	for i := 0; i < 10; i++ {
		blk.Points[10][5+i] = 1
	}
	for i := 0; i < 7; i++ {
		blk.Points[20+i][10] = 2
	}

	testMessage(t, message.Types["chunk_update"], func(w io.Writer) error {
		return builder.SendChunkUpdate(w, tick, blk)
	}, func(conn *message.Conn, t *testing.T) {
		rt, rblk := handler.ReadChunkUpdate(conn)
		if rt != tick {
			t.Fatal("Sent tick", tick, "but received", rt)
		}

		for i := range blk.Points {
			for j := range blk.Points[i] {
				pt := blk.Points[i][j]
				rpt := rblk.Points[i][j]
				if rpt != pt {
					t.Fatal("Sent point", pt, " at ", i, j, "but received", rpt)
				}
			}
		}
	})
}
Example #2
0
func TestActionsDone(t *testing.T) {
	tick := message.Tick(42)

	action1 := message.NewAction()
	action1.Id = 25
	action1.Initiator = 65

	actions := []*message.Action{action1}
	// TODO: more actions tests, with action params

	testMessage(t, message.Types["actions_done"], func(w io.Writer) error {
		return builder.SendActionsDone(w, tick, actions)
	}, func(conn *message.Conn, t *testing.T) {
		rt, ractions := handler.ReadActionsDone(conn)
		if rt != tick {
			t.Fatal("Sent tick", tick, "but received", rt)
		}

		if len(actions) != len(ractions) {
			t.Fatal("Sent", len(actions), "actions but received", len(ractions))
		}
		for i, action := range actions {
			raction := ractions[i]
			if !action.Equals(raction) {
				t.Fatal("Sent action", action, "but received", raction)
			}
		}
	})
}
Example #3
0
func TestEntityDestroy(t *testing.T) {
	tick := message.Tick(42)
	entityId := message.EntityId(93)

	testMessage(t, message.Types["entity_destroy"], func(w io.Writer) error {
		return builder.SendEntityDestroy(w, tick, entityId)
	}, func(conn *message.Conn, t *testing.T) {
		rt, id := handler.ReadEntityDestroy(conn)
		if rt != tick {
			t.Fatal("Sent tick", tick, "but received", rt)
		}
		if entityId != id {
			t.Fatal("Sent entity id", entityId, "but received", id)
		}
	})
}
Example #4
0
func TestEntitiesUpdate(t *testing.T) {
	tick := message.Tick(42)

	entity1 := message.NewEntity()
	entity1.Id = 69
	entity1.Position.X = 22
	entity1.Position.Y = 65
	diff1 := message.NewEntityDiff()
	diff1.Position = true

	entity2 := message.NewEntity()
	entity2.Id = 76
	entity2.Speed.Angle = 67
	entity2.Speed.Norm = 43
	diff2 := message.NewEntityDiff()
	diff2.SpeedAngle = true
	diff2.SpeedNorm = true

	entities := []*message.Entity{entity1, entity2}
	diffs := []*message.EntityDiff{diff1, diff2}

	testMessage(t, message.Types["entities_update"], func(w io.Writer) error {
		return builder.SendEntitiesUpdate(w, tick, entities, diffs)
	}, func(conn *message.Conn, t *testing.T) {
		rt, rentities, rdiffs := handler.ReadEntitiesUpdate(conn)
		if rt != tick {
			t.Fatal("Sent tick", tick, "but received", rt)
		}

		if len(entities) != len(rentities) {
			t.Fatal("Sent", len(entities), "entities but received", len(rentities))
		}
		for i, entity := range entities {
			diff := diffs[i]
			rentity := rentities[i]
			rdiff := rdiffs[i]

			if !diff.Equals(rdiff) {
				t.Fatal("Sent diff", diff, " at offset", i, "but received", rdiff)
			}
			if !entity.EqualsWithDiff(rentity, diff) {
				t.Fatal("Sent entity", entity, " at offset", i, "with diff", diff, "but received", rentity)
			}
		}
	})
}
Example #5
0
func TestChunksUpdate(t *testing.T) {
	tick := message.Tick(42)

	blk1 := message.NewBlock()
	for i := 0; i < 10; i++ {
		blk1.Points[10][5+i] = 1
	}
	for i := 0; i < 7; i++ {
		blk1.Points[20+i][10] = 2
	}

	blk2 := message.NewBlock()
	blk2.Fill(1)
	blk2.Points[10][5] = 0
	blk2.Points[7][21] = 2

	blks := []*message.Block{blk1, blk2}

	testMessage(t, message.Types["chunks_update"], func(w io.Writer) error {
		return builder.SendChunksUpdate(w, tick, blks)
	}, func(conn *message.Conn, t *testing.T) {
		rt, rblks := handler.ReadChunksUpdate(conn)
		if rt != tick {
			t.Fatal("Sent tick", tick, "but received", rt)
		}

		if len(rblks) != len(blks) {
			t.Fatal("Sent", len(blks), "blocks, but received", len(rblks))
		}
		for k, blk := range blks {
			rblk := rblks[k]
			for i := range blk.Points {
				for j := range blk.Points[i] {
					pt := blk.Points[i][j]
					rpt := rblk.Points[i][j]
					if rpt != pt {
						t.Fatal("Sent point", pt, " at ", i, j, "in block", k, "but received", rpt)
					}
				}
			}
		}
	})
}
Example #6
0
func TestLoginResponse_Ok(t *testing.T) {
	code := message.LoginResponseCodes["ok"]
	tick := message.Tick(42)
	time := time.Unix(70817303, 868000)

	testMessage(t, message.Types["login_response"], func(w io.Writer) error {
		return builder.SendLoginResp(w, code, tick, time)
	}, func(conn *message.Conn, t *testing.T) {
		receivedCode, receivedTick, receivedTime := handler.ReadLoginResponse(conn)
		if receivedCode != code {
			t.Fatal("Sent code", code, "but received", receivedCode)
		}
		if receivedTick != tick {
			t.Fatal("Sent tick", tick, "but received", receivedTick)
		}
		if !receivedTime.Equal(time) {
			t.Fatal("Sent time", time, "but received", receivedTime)
		}
	})
}
Example #7
0
func TestChatReceive(t *testing.T) {
	tick := message.Tick(42)
	username := "******"
	msg := "How r u guys? aéoaéoaèo"

	testMessage(t, message.Types["chat_receive"], func(w io.Writer) error {
		return builder.SendChatReceive(w, tick, username, msg)
	}, func(conn *message.Conn, t *testing.T) {
		rt, rusername, rmsg := handler.ReadChatReceive(conn)
		if rt != tick {
			t.Fatal("Sent tick", tick, "but received", rt)
		}
		if rusername != username {
			t.Fatal("Sent username", username, "but received", rusername)
		}
		if rmsg != msg {
			t.Fatal("Sent message", msg, "but received", rmsg)
		}
	})
}
Example #8
0
func TestMetaAction_PlayerLeft(t *testing.T) {
	code := message.MetaActionCodes["player_left"]
	tick := message.Tick(42)
	entityId := message.EntityId(69)

	testMessage(t, message.Types["meta_action"], func(w io.Writer) error {
		return builder.SendPlayerLeft(w, tick, entityId)
	}, func(conn *message.Conn, t *testing.T) {
		rt, id, c, _ := handler.ReadMetaAction(conn)
		if rt != tick {
			t.Fatal("Sent tick", tick, "but received", rt)
		}
		if id != entityId {
			t.Fatal("Sent entity id", entityId, "but received", id)
		}
		if c != code {
			t.Fatal("Sent code", code, "but received", c)
		}
	})
}
Example #9
0
func TestEntityCreate(t *testing.T) {
	tick := message.Tick(42)
	entity := message.NewEntity()
	entity.Id = 69
	entity.Position.X = 22
	entity.Position.Y = 67
	entity.Speed.Angle = 67
	entity.Speed.Norm = 78
	entity.Sprite = 12
	// TODO: populate other attributes

	testMessage(t, message.Types["entity_create"], func(w io.Writer) error {
		return builder.SendEntityCreate(w, tick, entity)
	}, func(conn *message.Conn, t *testing.T) {
		rt, e := handler.ReadEntityCreate(conn)
		if rt != tick {
			t.Fatal("Sent tick", tick, "but received", rt)
		}
		if !entity.Equals(e) {
			t.Fatal("Sent entity", entity, "but received", e)
		}
	})
}
Example #10
0
func TestMetaAction_PlayerJoined(t *testing.T) {
	code := message.MetaActionCodes["player_joined"]
	tick := message.Tick(42)
	entityId := message.EntityId(69)
	username := "******"

	testMessage(t, message.Types["meta_action"], func(w io.Writer) error {
		return builder.SendPlayerJoined(w, tick, entityId, username)
	}, func(conn *message.Conn, t *testing.T) {
		rt, id, c, u := handler.ReadMetaAction(conn)
		if rt != tick {
			t.Fatal("Sent tick", tick, "but received", rt)
		}
		if id != entityId {
			t.Fatal("Sent entity id", entityId, "but received", id)
		}
		if c != code {
			t.Fatal("Sent code", code, "but received", c)
		}
		if u != username {
			t.Fatal("Sent username", username, "but received", u)
		}
	})
}
Example #11
0
func TestService(t *testing.T) {
	s := clock.NewService()

	if s.GetAbsoluteTick() != 0 {
		t.Error("Absolute tick not initialized at 0")
	}
	if s.GetRelativeTick() != 0 {
		t.Error("Relative tick not initialized at 0")
	}

	ticks := 42
	for i := 0; i < ticks; i++ {
		s.Tick()
	}

	if int(s.GetAbsoluteTick()) != ticks {
		t.Error("Absolute tick not incremented, expected", ticks, "but got", s.GetAbsoluteTick())
	}
	if int(s.GetRelativeTick()) != ticks {
		t.Error("Relative tick not incremented, expected", ticks, "but got", s.GetRelativeTick())
	}

	// |--------------------|------------>
	//       ^     ^
	//     tick   now
	relTick := message.Tick(ticks - 6)
	absTick := s.ToAbsoluteTick(relTick)
	if int(absTick) != int(relTick) {
		t.Error("Invalid absolute tick conversion, expected", relTick, "but got", absTick)
	}

	// |--------------------|------------>
	//             ^     ^
	//            now  tick
	// Invalid tick: in the future
	relTick = message.Tick(9304)
	absTick = s.ToAbsoluteTick(relTick)
	if absTick != 0 {
		t.Error("Invalid absolute tick conversion, expected 0 as it is an invalid tick, but got", absTick)
	}

	for i := 0; i < message.MaxTick; i++ {
		s.Tick()
	}

	if int(s.GetAbsoluteTick()) != ticks+message.MaxTick {
		t.Error("Invalid absolute tick after message.MaxTick ticks, expected", 1+message.MaxTick, "but got", s.GetAbsoluteTick())
	}
	if int(s.GetRelativeTick()) != ticks {
		t.Error("Invalid relative tick after message.MaxTick ticks, expected", 1, "but got", s.GetRelativeTick())
	}

	// |--------------------|------------>
	//                          ^     ^
	//                        tick   now
	relTick = message.Tick(6)
	absTick = s.ToAbsoluteTick(relTick)
	if int(absTick) != message.MaxTick+6 {
		t.Error("Invalid absolute tick conversion, expected", message.MaxTick+6, "but got", absTick)
	}

	// |--------------------|------------>
	//                 ^               ^
	//               tick             now
	relTick = message.Tick(message.MaxTick - 42)
	absTick = s.ToAbsoluteTick(relTick)
	if int(absTick) != int(relTick) {
		t.Error("Invalid absolute tick conversion, expected", relTick, "but got", absTick)
	}
}