我的ThreadLocal包含并始终返回null(My ThreadLocal contains and returns null always)
我想知道当我将threadlocal.set()设置为32个元素的集合时,它是如何存储数据的。 ThreadLocal.get()始终返回null; 对应的FutureTask对象有一个结果属性= NullPointerException。 知道为什么ThreadLocal无法存储集合项吗?
public class MyCallable<T> implements Callable<Collection<T>> { public MyCallable( Collection<T> items ){ tLocal = new ThreadLocal<Collection<T>>(); tLocal.set( items ); //SETS NULL ALTHOUGH the PARAMETER CONTAINS 32 ITEMS } @Override @SuppressWarnings("unchecked") public Collection<T> call() throws Exception { synchronized( lock ){ ArrayList<T> _items = new ArrayList<T>(); ArrayList<T> _e = ( ArrayList<T> ) tLocal.get(); //RETURNS NULL for( T item : _e ){ _items = getPValue( item )); } return _items ; } } private ThreadLocal<Collection<T>> tLocal; private final Object lock = new Object(); }
用法片段:
List<Future<Collection<T>>> futures = new ArrayList<Future<Collection<T>>>(); ExecutorService pool = Executors.newFixedThreadPool( 8 ); for( int x = 0; x < numBatches; ++x ){ List<T> items = retrieveNext32Items( x ); futures.add( pool.submit( new MyCallable<T>( items )); } pool.shutdown(); for( Future<Collection<T>> future : futures ) { _items.addAll( future.get() ); //future.outcome = NullPointerException } return _items }
I am wondering how the threadlocal.set() has no data stored when I set it to a collection of 32 elements. ThreadLocal.get() returns null always; and the correspoding FutureTask object has an outcome property = NullPointerException. Any idea why ThreadLocal is unable to store collection items?
public class MyCallable<T> implements Callable<Collection<T>> { public MyCallable( Collection<T> items ){ tLocal = new ThreadLocal<Collection<T>>(); tLocal.set( items ); //SETS NULL ALTHOUGH the PARAMETER CONTAINS 32 ITEMS } @Override @SuppressWarnings("unchecked") public Collection<T> call() throws Exception { synchronized( lock ){ ArrayList<T> _items = new ArrayList<T>(); ArrayList<T> _e = ( ArrayList<T> ) tLocal.get(); //RETURNS NULL for( T item : _e ){ _items = getPValue( item )); } return _items ; } } private ThreadLocal<Collection<T>> tLocal; private final Object lock = new Object(); }
usage snippet:
List<Future<Collection<T>>> futures = new ArrayList<Future<Collection<T>>>(); ExecutorService pool = Executors.newFixedThreadPool( 8 ); for( int x = 0; x < numBatches; ++x ){ List<T> items = retrieveNext32Items( x ); futures.add( pool.submit( new MyCallable<T>( items )); } pool.shutdown(); for( Future<Collection<T>> future : futures ) { _items.addAll( future.get() ); //future.outcome = NullPointerException } return _items }
原文:https://stackoverflow.com/questions/31958871
最满意答案
您最好的选择是使用量角器的内置命令行选项来传递浏览器或在任何此类功能之间切换。
Usage: protractor [configFile] [options] configFile defaults to protractor.conf.js The [options] object will override values from the config file. See the reference config for a full list of options. Options: --help Print Protractor help menu --version Print Protractor version --browser, --capabilities.browserName Browsername, e.g. chrome or firefox
如果您查看量角器的cli选项 ,如果您在multicapabilties选项中设置了多个浏览器,则可以传递这样的浏览器名称 -
protractor config.js --capabilities.browserName='chrome' protractor config.js --capabilities.browserName='firefox'
您可以将其设置为package.json中的单个脚本,以在各种浏览器中运行测试 -
"scripts": { "tsc": "tsc", "test": "protractor ./config.js", "chrome-tests": "protractor ./config.js --capabilities.browserName='chrome'", "firefox-tests": "protractor ./config.js --capabilities.browserName='firefox'" }
现在你可以使用
npm
来调用它 -npm run chrome-tests // it would run your tests in chrome browser npm run firefox-tests // it would run your tests in firefox browser
您还可以在
conf
文件中使用params
对象传递参数,并在测试或命令行中的任何位置访问它们。params: { primaryBrowser: 'chrome' // I am biased towards chrome :) secondaryBrowser: 'firefox' },
您可以通过起诉浏览器的全局对象在测试中访问它们 -
console.log(browser.params.primaryBrowser); console.log(browser.params.secondaryBrowser);
同样,您可以在命令行中更改它们 -
protractor config.js --params.primaryBrowser='firefox'
通过
getMultiCapabilities
有一种更优雅的方式来做这些事情 -您甚至可以通过访问上述函数对象来传递多个浏览器,请参阅此链接以获取更多详细信息。
Your best bet would be to use protractor's in-built command line options for passing browsers or toggling between any such capabilities.
Usage: protractor [configFile] [options] configFile defaults to protractor.conf.js The [options] object will override values from the config file. See the reference config for a full list of options. Options: --help Print Protractor help menu --version Print Protractor version --browser, --capabilities.browserName Browsername, e.g. chrome or firefox
If you look at the protractor's cli options and if you have more than one browsers setup in your multicapabilties option, you can pass browser names like this -
protractor config.js --capabilities.browserName='chrome' protractor config.js --capabilities.browserName='firefox'
You can set this as individual scripts in your package.json to run tests in various browsers-
"scripts": { "tsc": "tsc", "test": "protractor ./config.js", "chrome-tests": "protractor ./config.js --capabilities.browserName='chrome'", "firefox-tests": "protractor ./config.js --capabilities.browserName='firefox'" }
Now you can invoke this by using
npm
-npm run chrome-tests // it would run your tests in chrome browser npm run firefox-tests // it would run your tests in firefox browser
You can also make use
params
object in yourconf
file pass paramaters and access them anywhere in your tests or command line.params: { primaryBrowser: 'chrome' // I am biased towards chrome :) secondaryBrowser: 'firefox' },
You can access them in your tests by suing browser's global object -
console.log(browser.params.primaryBrowser); console.log(browser.params.secondaryBrowser);
Similarly you can change them in your command line-
protractor config.js --params.primaryBrowser='firefox'
There is one more elegant way of doing such things through
getMultiCapabilities
-You can even pass multiple browsers by accessing the above functions object, please refer this link for more details.
Is there any way to pass multiple browser via protractor cli
相关问答
更多-
通过标签使用黄瓜VCR(Using VCR with Cucumber via tags)[2021-08-20]
您必须将VCR设置为忽略本地主机请求 。 否则,当水豚试图从您的网站请求任何页面时,VCR将阻止它。 将c.ignore_localhost = true添加到您的VCR配置块。 VCR.config do |c| c.cassette_library_dir = 'features/fixtures/vcr_cassettes' c.stub_with :fakeweb c.ignore_localhost = true end You have to set VCR to ignore l ... -
在运行rspec时,无法加载这样的文件 - cucumber-rails(getting cannot load such file — cucumber-rails when running rspec)[2023-07-13]
更改 gem 'cucumber-rails', :require => true 至 gem 'cucumber-rails', :require => false change gem 'cucumber-rails', :require => true to gem 'cucumber-rails', :require => false -
我通过使用钩子来设置变量来解决这个问题,如果您的标签与驱动程序没有关联,这可能会很有用: Before('@mobile') do @mobile = true end When /^I go to the homepage$/ do if @mobile visit "m.mysite.com" else visit "www.mysite.com" end end I got around this by using hooks to ...
-
它们也在webrat中得到明确支持。 即使您无法在黄瓜中找到内置支持,您也可以随时进入自己的步骤定义。 来源: http : //cheat.errtheblog.com/s/webrat/ == Assertions # check for text in the body of html tags # can be a string or regexp assert_contain("BURNINATOR") assert_contain(/trogdor/i) assert_ ...
-
您使用的是哪个版本的IntelliJ? 您是否在IntelliJ中安装了Ruby和Base Cucumber插件? (来自Intellij - >首选项 - >插件)。 如果没有,第一步就是安装它们。 然后,您可以通过“运行”>“编辑配置”创建运行配置,并选择所需的详细信息。 如果您还有其他问题,可以使用屏幕上的配置(从“运行”>“编辑配置”)。 I didn't ever get to the bottom of exactly what the issue was, but completely rem ...
-
在你的app/config/application.yml你有黄瓜段吗? 如果没有,添加这个: cucumber: <<: *defaults In your app/config/application.yml do you have a cucumber section? If not, add this: cucumber: <<: *defaults
-
在基本设置中,您不能,因为您需要提供示例框架,并且您不能在1个默认配置文件中拥有2个框架。 你可以做的是使用命令行参数和像yargs这样的cli工具,并做这样的事情。 如果你通过例如npm script运行量角器,你可以做这样的事情 npm run e2e -- --bdd // the commmand line tool const argv = require('yargs').argv; // place you default config here, that should hold a ...
-
Webdriverio(WDIO)和基于Tag的Cucumber浏览器(Webdriverio (WDIO) and Cucumber Browser based on Tag)[2023-05-18]
定义浏览器配置时, 可以指定某个测试仅在该浏览器中运行 。 When you define your browser configuration, you can specify certain test to run in that browser only. -
您最好的选择是使用量角器的内置命令行选项来传递浏览器或在任何此类功能之间切换。 Usage: protractor [configFile] [options] configFile defaults to protractor.conf.js The [options] object will override values from the config file. See the reference config for a full list of options. Options: --help ...
-
我们是否可以列出属于Cucumber标签的所有场景(Can we list all the scenarios which fall under a Cucumber tag)[2023-04-07]
您可以在CucumberOptions使用dryRun=true选项,并在运行器中使用标记过滤器来获取报告中的方案列表。 此选项不会执行您的功能,但会列出它们并检查步骤是否具有相应的胶水代码。 @CucumberOptions(plugin={"pretty", "html:report"}, tags={"@smoke"}, snippets=SnippetType.CAMELCASE, glue="....", features="src/test/resources/feature ...