{"id":10410,"date":"2022-09-23T15:56:02","date_gmt":"2022-09-23T10:26:02","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/"},"modified":"2022-09-23T15:56:02","modified_gmt":"2022-09-23T10:26:02","slug":"solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/","title":{"rendered":"[Solved] Netty how to test Handler which uses Remote Address of a client"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-63420638\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"63420638\" data-parentid=\"63209649\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Two things that could help:<\/p>\n<ol>\n<li>\n<p>Do not annotate with <code>@ChannelHandler.Sharable<\/code> if your handler is NOT sharable. This can be misleading. Remove unnecessary state from handlers. In your case you should remove the <code>remoteAddress<\/code> member variable and ensure that <code>Gson<\/code> and <code>CarParkPermissionService<\/code> can be reused and are thread-safe.<\/p>\n<\/li>\n<li>\n<p><code>\"Your remote address is embedded\"<\/code> is NOT an error. It actually is the message written by your handler onto the outbound channel (cf. your <code>channelActive()<\/code> method)<\/p>\n<\/li>\n<\/ol>\n<p>So it looks like it could work.<\/p>\n<h2>EDIT<\/h2>\n<p>Following your comments here are some clarifications regarding the second point. I mean that:<\/p>\n<ul>\n<li>your code making use of <code>EmbeddedChannel<\/code> is almost correct. There is just a misunderstanding on the expected results (assert).<\/li>\n<\/ul>\n<p>To make the unit test successful, you just have either:<\/p>\n<ul>\n<li>to comment this line in <code>channelActive()<\/code>: <code>ctx.writeAndFlush(\"Your remote ...\")<\/code><\/li>\n<li>or to poll the second message from <code>Queue&lt;Object&gt; outboundMessages<\/code> in <code>testHeartbeatMessage()<\/code><\/li>\n<\/ul>\n<p>Indeed, when you do this:<\/p>\n<pre><code>\/\/ when\nembeddedChannel.writeInbound(request);\n<\/code><\/pre>\n<p><strong>(1)<\/strong> You actually open the channel once, which fires a <code>channelActive()<\/code> event. You don&#8217;t have a log in it but we see that the variable <code>remoteAddress<\/code> is not null afterwards, meaning that it was assigned in the <code>channelActive()<\/code> method.<\/p>\n<p><strong>(2)<\/strong> At the end of the <code>channelActive()<\/code> method, you eventually already send back a message by writing on the channel pipeline, as seen at this line:<\/p>\n<pre><code>ctx.writeAndFlush(\"Your remote address is \" + remoteAddress + \".\\r\\n\");\n\/\/ In fact, this is the message you see in your failed assertion.\n<\/code><\/pre>\n<p><strong>(3)<\/strong> Then the message written by <code>embeddedChannel.writeInbound(request)<\/code> is received and can be read, which fires a <code>channelRead()<\/code> event. This time, we see this in your log output:<\/p>\n<pre><code>22:21:29.062 [main] INFO handler.ProcessingHandler - CLIENT_IP: embedded\n22:21:29.062 [main] INFO handler.ProcessingHandler - CLIENT_REQUEST: {\"messageID\":\"heartbeat\"}\n22:21:29.067 [main] DEBUG handler.ProcessingHandler - heartbeat request: HeartbeatRequest(messageID=heartbeat)\n<\/code><\/pre>\n<p><strong>(4)<\/strong> At the end of <code>channelRead(ChannelHandlerContext ctx, Object msg)<\/code>, you will then send a <strong>second<\/strong> message (the expected one):<\/p>\n<pre><code>HeartbeatResponse response = HeartbeatResponse.builder()\n     .responseCode(\"ok\")\n     .build();\nctx.writeAndFlush(response + \"\\n\\r\");\n<\/code><\/pre>\n<p>Therefore, with the following code of your unit test&#8230;<\/p>\n<pre><code>Queue&lt;Object&gt; outboundMessages = embeddedChannel.outboundMessages();\nassertEquals(expected, outboundMessages.poll());\n<\/code><\/pre>\n<p>&#8230; you should be able to <code>poll()<\/code> <strong>two<\/strong> messages:<\/p>\n<ul>\n<li><code>\"Your remote address is embedded\"<\/code><\/li>\n<li><code>\"{ResponseCode\":\"ok\"}<\/code><\/li>\n<\/ul>\n<p>Does it make sense for you?<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">9<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Netty how to test Handler which uses Remote Address of a client <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Two things that could help: Do not annotate with @ChannelHandler.Sharable if your handler is NOT sharable. This can be misleading. Remove unnecessary state from handlers. In your case you should remove the remoteAddress member variable and ensure that Gson and CarParkPermissionService can be reused and are thread-safe. &#8220;Your remote address is embedded&#8221; is NOT &#8230; <a title=\"[Solved] Netty how to test Handler which uses Remote Address of a client\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/\" aria-label=\"More on [Solved] Netty how to test Handler which uses Remote Address of a client\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[999,1914,2860,974,1514],"class_list":["post-10410","post","type-post","status-publish","format-standard","hentry","category-solved","tag-handler","tag-ip-address","tag-netty","tag-spring-boot","tag-unit-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Netty how to test Handler which uses Remote Address of a client - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Netty how to test Handler which uses Remote Address of a client - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Two things that could help: Do not annotate with @ChannelHandler.Sharable if your handler is NOT sharable. This can be misleading. Remove unnecessary state from handlers. In your case you should remove the remoteAddress member variable and ensure that Gson and CarParkPermissionService can be reused and are thread-safe. &quot;Your remote address is embedded&quot; is NOT ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-23T10:26:02+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Netty how to test Handler which uses Remote Address of a client\",\"datePublished\":\"2022-09-23T10:26:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/\"},\"wordCount\":279,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"handler\",\"ip-address\",\"netty\",\"spring-boot\",\"unit-testing\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/\",\"name\":\"[Solved] Netty how to test Handler which uses Remote Address of a client - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-23T10:26:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Netty how to test Handler which uses Remote Address of a client\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Netty how to test Handler which uses Remote Address of a client - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Netty how to test Handler which uses Remote Address of a client - JassWeb","og_description":"[ad_1] Two things that could help: Do not annotate with @ChannelHandler.Sharable if your handler is NOT sharable. This can be misleading. Remove unnecessary state from handlers. In your case you should remove the remoteAddress member variable and ensure that Gson and CarParkPermissionService can be reused and are thread-safe. \"Your remote address is embedded\" is NOT ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/","og_site_name":"JassWeb","article_published_time":"2022-09-23T10:26:02+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Netty how to test Handler which uses Remote Address of a client","datePublished":"2022-09-23T10:26:02+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/"},"wordCount":279,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["handler","ip-address","netty","spring-boot","unit-testing"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/","url":"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/","name":"[Solved] Netty how to test Handler which uses Remote Address of a client - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-23T10:26:02+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-netty-how-to-test-handler-which-uses-remote-address-of-a-client\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Netty how to test Handler which uses Remote Address of a client"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=10410"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10410\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=10410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=10410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=10410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}