0

I am writing a test (groovy language\spock framework if relevant)

Initially it makes our application send an email to a specific exchange (office 365 if relevant) mailbox. After that I need to log into that mailbox and verify the email that the application has sent.

I use EWS Java API to communicate with the EWS (com.microsoft.ews-java-api 2.0)

After connecting to the exchange service, i do the following:

SearchFilter searchFilter = new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "search string")
Awaitility.await()
            .atMost(180, TimeUnit.SECONDS)
            .until({
        exchangeService.findItems(WellKnownFolderName.Inbox, searchFilter, new ItemView(1)).totalCount > 0
    })

Awaitility.await() in here is essentially a dynamic wait - it retries the code until conditions are met. It can be replaced with any kind of loop with a thread sleep and break condition.

On the first attempt the findItems call never finds the email - it usually takes around a minute for the email to be sent\arrive.

The issue is that on any subsequent attempt the findItems does not find the item successfully. However if I hold a breakpoint on it until I see the email in the mailbox - it works successfully.

So for me it looks like once the search fails (does not find anything) executing the method again does not do anything. What am I doing wrong?

Ubeogesh
  • 1,633
  • 2
  • 15
  • 22
  • I am seeing this same problem. Looping the findItems call wont return any results if the first execution doesn't return anything. I have just tried newing up the exchangeService each time to see if that maybe helps/prevents caching somehow? – Lee Tickett Oct 14 '19 at 14:48

1 Answers1

0

I would suggest you put a sleep between making the FindItem calls as your not giving the Restriction on the Server time to update between subsequent call. Also your SearchFilter is very resource intensive because its doing a Substring of the body of the Message. If you want to track delivery of a Message why don't you set the InternetMessageId when you send the Message Internet Message ID FROM EWS Managed API Send Email c# then create a SearchFilter that only find Message with that Id.

Glen Scales
  • 20,495
  • 1
  • 20
  • 23
  • Sleep is already there - the Awaitility.await() does that (incrementally longer sleeps each time condition inside fails). As I wrote - I am doing a test. Unfortunately the system under test does not track the internet message ID anywhere; besides, the test is to be performed like once a day, and the mailbox does not contain a lot of emails (and it's office 365 so I couldn't care less about the load) – Ubeogesh Jan 15 '18 at 08:12
  • what is the "restriction on the server time" that you mentioned? – Ubeogesh Jan 15 '18 at 08:16
  • When you create a SearchFilter EWS will create a temporary restriction see https://technet.microsoft.com/en-us/library/cc535025(exchg.80).aspx for more details. An alternative to using a SearchFilter would be to use AQS https://msdn.microsoft.com/en-us/library/office/ee693615(v=exchg.150).aspx (which has ClearCache attribute). Or if the text your searching for in the body is static create a SearchFolder and poll that folder instead. – Glen Scales Jan 16 '18 at 00:44