ErlangPid extends ErlangTerm { public final NodeDescriptor getDescriptor () { if (descriptor == null) { descriptor = NodeDescriptor.from(node.asText()); } return descriptor; } ErlangPid(TermType type, @NonNull ByteBuf buffer); @Builder private  ErlangPid(TermType type, @NonNull String node, int id, int serial, int creation); @Override String asText(String defaultValue); @Override ErlangPid asPid(); @Override String toString(); final NodeDescriptor getDescriptor();  }
@Test public void newInstance () { val node = "popa"; val id = 500; val serial = 10; val creation = 42; val bytes = Bytes.resizableArray() .write1B(PID.getCode()) .writeNB(new ErlangAtom(node).toBytes()) .write4B(id & 0x7FFF) .write4B(serial & 0x1FFF) .write1B(creation & 0x3) .arrayCopy(); ErlangPid pid = ErlangTerm.newInstance(wrappedBuffer(bytes)); assertThat(pid).isNotNull(); SoftAssertions.assertSoftly(softly -> { softly.assertThat(pid.getDescriptor().getNodeName()) .isEqualTo(node); softly.assertThat(pid.getId()) .isEqualTo(id & 0x7FFF); softly.assertThat(pid.getSerial()) .isEqualTo(serial & 0x1FFF); softly.assertThat(pid.getCreation()) .isEqualTo(creation & 0x3); }); }
@Test public void decode () throws Exception { byte[] bytes1 = Bytes.resizableArray() .write1B(PID.getCode()) .writeNB(new ErlangAtom("popa@localhost").toBytes()) .write4B(Integer.MAX_VALUE) .write4B(Integer.MAX_VALUE) .write1B(Integer.MAX_VALUE) .arrayCopy(); try (val input = new OtpInputStream(bytes1)) { ErlangPid pid = ErlangTerm.newInstance(wrappedBuffer(bytes1)); OtpErlangPid otpPid = input.read_pid(); assertThat(pid.getDescriptor().getFullName()) .isEqualTo(otpPid.node()); assertThat(pid.getId()) .isEqualTo(otpPid.id()); assertThat(pid.getCreation()) .isEqualTo(otpPid.creation()); assertThat(pid.getSerial()) .isEqualTo(otpPid.serial()); } byte[] bytes2 = Bytes.resizableArray() .write1B(NEW_PID.getCode()) .writeNB(new ErlangAtom("popa@localhost").toBytes()) .write4B(Integer.MAX_VALUE) .write4B(Integer.MAX_VALUE) .write4B(Integer.MAX_VALUE) .arrayCopy(); try (val input = new OtpInputStream(bytes2)) { ErlangPid pid = ErlangTerm.newInstance(wrappedBuffer(bytes2)); OtpErlangPid otpPid = input.read_pid(); assertThat(pid.getDescriptor().getFullName()) .isEqualTo(otpPid.node()); assertThat(pid.getId()) .isEqualTo(otpPid.id()); assertThat(pid.getCreation()) .isEqualTo(otpPid.creation()); assertThat(pid.getSerial()) .isEqualTo(otpPid.serial()); } }
@Test public void equality () { val pid1 = ErlangPid.builder() .type(PID) .node("node1") .id(28) .serial(97) .creation(3) .build(); pid1.getDescriptor(); val pid2 = ErlangPid.builder() .type(NEW_PID) .node("node1") .id(28) .serial(97) .creation(3) .build(); val pid3 = ErlangPid.builder() .type(NEW_PID) .node("node1") .id(28) .serial(96) .creation(3) .build(); val pid4 = ErlangPid.builder() .type(NEW_PID) .node("node2") .id(28) .serial(96) .creation(3) .build(); assertThat(pid1).isEqualTo(pid2); assertThat(pid2).isNotEqualTo(pid3); assertThat(pid3).isNotEqualTo(pid4); }