async

How do I test an async method with NUnit (or possibly with another framework)?

匿名 (未验证) 提交于 2019-12-03 02:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have an ASP.NET Web API application, with an ApiController that features asynchronous methods, returning Task<> objects and marked with the async keyword. public class MyApiController : ApiController { public async Task GetDataById ( string id ) { ... } } How can I write NUnit tests for the ApiController's asynchronous methods? If I need to use another testing framework I'm open for that too. I'm fairly new to .NET unit testing in general, so I'm interested in learning best practices. 回答1: As of today (7/2/2014) async testing is

Async await in linq select

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need to modify an existing program and it contains following code: var inputs = events.Select(async ev => await ProcessEventAsync(ev)) .Select(t => t.Result) .Where(i => i != null) .ToList(); But this seems very weird to me, first of all the use of async and await in the select. According to this answer by Stephen Cleary I should be able to drop those. Then the second Select which selects the result. Doesn't this mean the task isn't async at all and is performed synchronously (so much effort for nothing), or will the task be performed

Finalizer for Parallel.ForEach

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: How do I add a finalizer that runs once all parallels have completed? Parallel . ForEach ( entries , new ParallelOptions { MaxDegreeOfParallelism = 15 }, async ( entry ) => // Do something with the entry. }); I have tried like this but it doesn't compile: Parallel . ForEach ( entries , new ParallelOptions { MaxDegreeOfParallelism = 15 }, async ( entry ) => // Do something with the entry. }, () => { // Was hoping this would work. }); 回答1: You should not declare the action for the Parallel.ForEach as async . If you use an await

Use an async callback with Task.ContinueWith

匿名 (未验证) 提交于 2019-12-03 02:14:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to play a bit with C#'s async/await/continuewith. My goal is to have to have 2 tasks which are running in parallel, though which task is executing a sequence of action in order. To do that, I planned to have a List<Task> that represent the 2 (or more) tasks running in parallel, and to use ContinueWith on each of the Task My problem is that the callback in continue with seems not to be executed while the await taskList has already returned. In order to summarize, here's a sample to illustrate what I'm expecting to happen: class

c++11 std::async doesn&#039;t work in mingw

匿名 (未验证) 提交于 2019-12-03 02:14:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Running this code from Herb Sutter's presentation . This works fine in linux under gcc 4.6.3. I'm thinking that future.h isn't supported in mingw, but the error is really hard to understand! #include <iostream> #include <vector> #include <string> #include <future> #include <algorithm> using namespace std; string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector<future<string>> v; v.push_back( async([]{ return flip(" ,olleH"); }) ); v.push_back( async([]{ return flip(".gnaL"); }) ); v.push_back( async([]{ return

How to enable request scope in async task executor

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In my app I have some async web services. Server accept request, return OK response and start processing request with AsyncTaskExecutor. My question is how to enable request scope here because in this processing I need to get class which is annotated by: @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) Now I get exception: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.requestContextImpl': Scope 'request' is not active for the current thread;

EF Data Context - Async/Await &amp; Multithreading

匿名 (未验证) 提交于 2019-12-03 02:12:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I frequently use async/await to ensure ASP.NET MVC Web API threads are not blocked by longer-running I/O and network operations, specifically database calls. The System.Data.Entity namespace provides a variety of helper extensions here, such as FirstOrDefaultAsync , ContainsAsync , CountAsync and so forth. However, since data contexts are not thread safe, this means that the following code is problematic: var dbContext = new DbContext (); var something = await dbContext . someEntities . FirstOrDefaultAsync ( e => e . Id == 1 ); var

await works but calling task.Result hangs/deadlocks

匿名 (未验证) 提交于 2019-12-03 02:09:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have the following four tests and the last one hangs when I run it, my question is why this happens: [Test] public void CheckOnceResultTest() { Assert.IsTrue(CheckStatus().Result); } [Test] public async void CheckOnceAwaitTest() { Assert.IsTrue(await CheckStatus()); } [Test] public async void CheckStatusTwiceAwaitTest() { Assert.IsTrue(await CheckStatus()); Assert.IsTrue(await CheckStatus()); } [Test] public async void CheckStatusTwiceResultTest() { Assert.IsTrue(CheckStatus().Result); // This hangs Assert.IsTrue(await CheckStatus()); }

Understanding execute async script in Selenium

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've been using selenium (with python bindings and through protractor mostly) for a rather long time and every time I needed to execute a javascript code, I've used execute_script() method. For example, for scrolling the page (python): driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") Or, for infinite scrolling inside an another element (protractor): var div = element(by.css('div.table-scroll')); var lastRow = element(by.css('table#myid tr:last-of-type')); browser.executeScript("return arguments[0].offsetTop;", lastRow

jasmine 2 - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

匿名 (未验证) 提交于 2019-12-03 02:07:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Having trouble with jasmine 2 and getting async specs wired up: define(['foo'], function(foo) { return describe('foo', function() { beforeEach(function(done) { window.jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; return setTimeout((function() { console.log('inside timeout'); return done(); }), window.jasmine.DEFAULT_TIMEOUT_INTERVAL); }); return it('passes', function() { return expect({}).toBeDefined(); }); }); }); When I run via karma, I get back Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT