Comments (11)
Instead of letting different layers register the same service, why not putting all the service registeration logic into one place?
from mobly.
If so, I'd suggest asking the utility owner to check for duplicate registration.
My main concern for your proposal is that it's in the base_service class, so this modification means duplicate registration is allowed and ignored for all services. This is true for uiatuomator, but not true for all services, e.g. service for screen recording, service for starting a user session with specific arguments. For these services, duplicate registration should trigger an error.
from mobly.
I'm ok with the new proposal if you are willing to create a PR for it.
@xpconanfan any objection to this public API change: Add an arg to ignore the registration error if the service class is already registered with the given alias.
from mobly.
from mobly.
@mhaoli ,
Good question! Actually, for our code base, we did put all the registration of services in one place. However, we couldn't avoid the other utilities (third-party) to register what ever services they want at what ever time they prefer.
Ps. Currently, our temporary approach is to unregister the uiautomator service right after instantiate the target object to avoid confliction at later time from our code base to register the uiautomator (Or we could also improve the registration process to check for duplicate registration as well.)
from mobly.
Hi @mhaoli ,
Your concern and comment is fair and well understood. What if we could provide one more argument to let user decides the behavior they want such as a allow_duplicate
? So we could decide which service we want to avoid exception and allow duplicate registration. e.g.:
def register(
self, alias, service_class, configs=None, start_service=True, allow_duplicate = False):
"""Registers a service.
This will create a service instance, starts the service, and adds the
instance to the mananger.
Args:
alias: string, the alias for this instance.
service_class: class, the service class to instantiate.
configs: (optional) config object to pass to the service class's
constructor.
start_service: bool, whether to start the service instance or not.
Default is True.
allow_duplicate: bool, True to ignore the registration of duplicate service.
"""
if not inspect.isclass(service_class):
raise Error(self._device, '"%s" is not a class!' % service_class)
if not issubclass(service_class, base_service.BaseService):
raise Error(
self._device,
'Class %s is not a subclass of BaseService!' % service_class,
)
# New code here
if all([
alias in self._service_objects,
self._service_objects[alias].__class__ == service_class,
allow_duplicate]):
logging.warning('Service with alias=%s has been registered as %s!', alias, service_class)
return
...
from mobly.
Hi @xpconanfan ,
For Just catch the exception in your own code and handle it?
That will work. But the try/catch or the code to examine if the
uiautomator
is already registered will be duplicate in many places. Because there might be more than one third party tools that will useuiautomator
or even more other services that our testing will register too and cause confliction. So with this feature, we could useallow_duplicate = True
to ignore those confliction in the root place.
Then for Doesn't look like something worth changing public apis for?
I think with feature, we could save a lot of duplicate code such as below code snippet (Or to add a complex try/catch to ensure the Error is caused by duplicate registration).
if 'uiautomator' in self._ad.services.list_live_services():
# Skip registration of 'uiautomator'
If it looks good to both of you, I will prepare a PR. Thanks!
from mobly.
from mobly.
Hi @xpconanfan ,
What with accessing private field (self._ad.services
) isn't a good practice, what with the default value of new argument allow_duplicate
is given as False which aligns with original behavior, I believe the original users won't be impacted. But the benefit is that we could use a more native way to get rid of this dup-registration confliction. Does it make sense to you? Thanks.
from mobly.
from mobly.
Hi @xpconanfan ,
You are right about "services" is not private. "_ad" is "private only bc you made it so, which doesn't make sense in the
. I made wrong assumption here. It should could be improved by our inner codebase.
If support a new argument doesn't sound like a good idea to you, let's return to the original issue, is it possible to let ServiceManager
support query of the registered alias name of a certain service class. Because below code snippet
if 'uiautomator' in self._ad.services.list_live_services():
# Skip registration of 'uiautomator'
will still have issue while the UI automator isn't registered with the name as uiautomator
.
So one idea is to have new method such as:
>>> dut = bttc.get('07311JECB08252', init_snippet_uiautomator=True)
>>> from snippet_uiautomator import uiautomator
>>> dut.services.get_service_name_by_class(uiautomator.UiAutomatorService)
'uiautomator'
PoC code of method get_service_name_by_class
:
def get_service_name_by_class(self, service_class) -> str | None:
"""Gets service name by service class."""
for alias, service_object in self._service_objects.items():
if service_object.__class__ == service_class:
return alias
return None
So we could use this method to confirm the registration of uiautomator.UiAutomatorService
and confirm the registered name is as what we expected.
from mobly.
Related Issues (20)
- Support method "take_snapshot" in AndroidDevice HOT 4
- Mobly exits without cleaning resources if receives a SIGTERM signal
- Can multiple devices execute concurrently? HOT 2
- Get error type from test summary HOT 1
- Drop Python 3.6 support
- Android snippet client should clean event client when stopping
- Remove Travis CI link from `README.md` HOT 1
- Remove Python 3.6 hook from CI HOT 2
- Question on using mobly to interact with multiple android displays ? HOT 6
- am getting error while running the hello_world_test.py script HOT 4
- Remove typing_extensions when we support Python >=3.8 HOT 1
- How to write test that handles USB disconnect and reconnect properly HOT 3
- Test failures on Python 3.11 HOT 1
- Allow regex matching for specifying test cases HOT 1
- Check that some views are present on Activity HOT 7
- Add abilty to run 'adb reverse' via Mobly
- No name 'snippet' in module 'mobly.snippet' HOT 5
- Unable to shutdown sl4a event dispatcher.
- How do i add ble pair and connect api in mobly ?
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 mobly.