Comments (14)
Good suggestion. Do you have any existing solution?
from moco.
Based on ServerSocket, you can specify a port of "0" to grab an available one, but I'm not 100% sure if Netty has the same concept. I'll take a look and let you know what I find.
from moco.
I tested Moco with port "0" and it seems that Netty works the same and just binds to any available port. Was able to confirm this by executing a future.channel().localAddress()
after the bootstrap.bind(..
in MocoServer.java
.
Looking for some advice from you Zheng for how the Moco interface should support this.
- Where should somebody be able to query the port that was actually bound to? Maybe the
HttpServer
object, but currently theHttpServer
interface does not support it.. onlyActualHttpServer
does which would mean a nasty typecast after callingMoco.httpserver(..)
. - How should
MocoServer.start
convey the port number to theHttpServer
? Should it have its owngetPort
method for others to query or some other way?
from moco.
@garrettheel I didn't figure out what is scenario for this. Why do you think it is a good idea to get port dynamically. I need to get port back and then set this port in my unit test case. Comparing to current usage, do you think it makes us easy?
from moco.
@dreamhead I think it depends on the scale of your project. If you have a small number of tests and are always going to run them on the same computer, you can probably hardcode a certain port and it will work OK.
However one scenario that breaks is if you were to speed up tests by running them concurrently. You will get random conflicts doing this unless you remember to use a different port for every test, which also increases your chances of conflicting with a port already used in the system.
Using something like this would be optional for those that need it, and I don't think it would be any harder than it is presently. For example:
HttpServer server = httpserver(0);
// ...
Content content = Request.Get("http://localhost:" + server.getLocalPort()).execute().returnContent();
assertThat(content.asString(), is("foo"));
What do you think?
from moco.
Sounds reasonable.
server.getLocalPort() only works in running block, because the port will be determined when server is started. I'm not sure whether this behaviour is acceptable.
from moco.
Seeing as it's following ServerSocket way of doing things, it could do the same thing if it's not bound yet. E.g:
Returns:
the port number to which this socket is listening or -1 if the socket is not bound yet.
Happy for you to suggest an alternative interface, though!
from moco.
The code has been committed with this issue. You can refer MocoPortTest for test cases. Any feedback is welcome.
On the other hand, Moco runner has supported this feature. The runner will pick up the port itself.
from moco.
@garrettheel , since we've already implemented this feature. What do you think if shutdown port is changed with this approach?
from moco.
@dreamhead Thanks for the implementation - it looks great. I think it would be good for shutdown port to use this approach too, as it's just one less thing to worry about and makes Moco easier to use. So long as there's a simple way to find out the port, such as server.getShutdownPort()
then it should be pretty easy for me to update the maven plugin too
from moco.
@garrettheel the code has been committed. RunnerFactory will return ShutdownRunner which has shutdownPort. The latest snapshot has been uploaded to repo. Try it in your plugin.
from moco.
@dreamhead I see what you mean - this is a good idea, I'll update the plugin.
I wonder if it's also a good idea to expose the server port from a Runner
so you can also have a random port when using the plugin for integration tests. The plugin could set a system property for which port Moco is running on so tests know where to send requests.
What do you think?
from moco.
One thing I am not sure about system property is when I try to run many Moco instances in one machine, all of them will set the same property.
from moco.
Fair point - by having a system property we'd be limiting one Moco instance per JVM (not per machine). Do you have any other suggestions?
from moco.
Related Issues (20)
- MOCO如何实现带参重定向 HOT 1
- socket请求实现多文件配置
- java.lang.IllegalArgumentException: unknown configuration
- Maven test fails to run moco server when using jacoco maven plugin 0.7.8 or newer #258
- Use URI path parameters on the response template HOT 6
- 返回值类型不支持xml HOT 2
- java.lang.IllegalArgumentException: unknown configuration :[Z@78073c4 HOT 2
- 请求参数中存在中文时,每次请求都报400,怎么进行解决? HOT 1
- 响应体中存在奇数个汉字的时候必定乱码 HOT 5
- 如何支持或的逻辑,以及如何支持对字段名采用正则匹配
- ERROR java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
- 请求超过10MB报错,有配置的地方吗 HOT 3
- 返回response json时,template无法使用
- 加载配置文件无法使用*.json HOT 5
- Asynchronous, Duplicate key Accept-Encoding HOT 3
- 希望能支持响应json直接使用模板语法
- The json in the configuration request needs to match all the json in the body in order for the test to pass HOT 2
- 功能请求: 支持Http SSE(Server-Send Events) API 相关mock能力 HOT 1
- Response header 同时配置content-type & content-length, 响应卡住
- How to configure multiple events for one request. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moco.