ErlangPort extends ErlangTerm { public final NodeDescriptor getDescriptor () { if (descriptor == null) { descriptor = NodeDescriptor.from(node.asText()); } return descriptor; } ErlangPort(TermType type, @NonNull ByteBuf buffer); @Builder private  ErlangPort(TermType type, @NonNull String node, int id, int creation); @Override String asText(String defaultValue); @Override ErlangPort asPort(); @Override String toString(); final NodeDescriptor getDescriptor();  }
@Test public void newInstance () { val node = "popa@localhost"; val id = 500; val creation = 42; val bytes = Bytes.resizableArray() .write1B(PORT.getCode()) .writeNB(new ErlangAtom(node).toBytes()) .write4B(id & 0xFFFFFFF) .write1B(creation & 0x3) .arrayCopy(); ErlangPort port = ErlangTerm.newInstance(wrappedBuffer(bytes)); assertThat(port).isNotNull(); SoftAssertions.assertSoftly(softly -> { softly.assertThat(port.getDescriptor().getFullName()) .isEqualTo(node); softly.assertThat(port.getId()) .isEqualTo(id & 0xFFFFFFF); softly.assertThat(port.getCreation()) .isEqualTo(creation & 0x3); }); }
@Test public void decode () throws Exception { byte[] bytes1 = Bytes.resizableArray() .write1B(PORT.getCode()) .writeNB(new ErlangAtom("popa@localhost").toBytes()) .write4B(Integer.MAX_VALUE) .write1B(Integer.MAX_VALUE) .arrayCopy(); try (val input = new OtpInputStream(bytes1)) { ErlangPort port = ErlangTerm.newInstance(wrappedBuffer(bytes1)); OtpErlangPort otpPid = input.read_port(); assertThat(port.getDescriptor().getFullName()) .isEqualTo(otpPid.node()); assertThat(port.getId()) .isEqualTo(otpPid.id()); assertThat(port.getCreation()) .isEqualTo(otpPid.creation()); } byte[] bytes2 = Bytes.resizableArray() .write1B(NEW_PORT.getCode()) .writeNB(new ErlangAtom("popa@localhost").toBytes()) .write4B(Integer.MAX_VALUE) .write4B(Integer.MAX_VALUE) .arrayCopy(); try (val input = new OtpInputStream(bytes2)) { ErlangPort port = ErlangTerm.newInstance(wrappedBuffer(bytes2)); OtpErlangPort otpPid = input.read_port(); assertThat(port.getDescriptor().getFullName()) .isEqualTo(otpPid.node()); assertThat(port.getId()) .isEqualTo(otpPid.id()); assertThat(port.getCreation()) .isEqualTo(otpPid.creation()); } }