Do not use deprecated mockito api
[idea/community.git] / plugins / stats-collector / test / com / intellij / stats / completion / PerformanceTests.kt
1 /*
2  * Copyright 2000-2017 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.stats.completion
17
18 import com.intellij.codeInsight.completion.LightFixtureCompletionTestCase
19 import com.intellij.openapi.application.ApplicationManager
20 import com.intellij.stats.network.service.RequestService
21 import com.intellij.stats.network.service.ResponseData
22 import com.intellij.stats.sender.StatisticSenderImpl
23 import com.intellij.stats.storage.FilePathProvider
24 import com.intellij.testFramework.UsefulTestCase
25 import org.mockito.ArgumentMatchers
26 import org.mockito.Mockito.*
27 import org.picocontainer.MutablePicoContainer
28 import java.io.File
29 import java.util.concurrent.atomic.AtomicBoolean
30
31
32 class PerformanceTests : LightFixtureCompletionTestCase() {
33     private lateinit var pathProvider: FilePathProvider
34     
35     private val runnable = "interface Runnable { void run();  void notify(); void wait(); void notifyAll(); }"
36     private val text = """
37 class Test {
38     public void run() {
39         Runnable r = new Runnable() {
40             public void run() {}
41         };
42         r<caret>
43     }
44 }
45 """
46
47     override fun setUp() {
48         super.setUp()
49         val container = ApplicationManager.getApplication().picoContainer as MutablePicoContainer
50         pathProvider = container.getComponentInstance(FilePathProvider::class.java.name) as FilePathProvider
51         CompletionTrackerInitializer.isEnabledInTests = true
52     }
53
54     override fun tearDown() {
55         CompletionTrackerInitializer.isEnabledInTests = false
56         try {
57             super.tearDown()
58         } finally {
59             CompletionLoggerProvider.getInstance().dispose()
60             val statsDir = pathProvider.getStatsDataDirectory()
61             statsDir.deleteRecursively()
62         }
63     }
64
65     fun `test do not block EDT on data send`() {
66         myFixture.configureByText("Test.java", text)
67         myFixture.addClass(runnable)
68
69         val requestService = slowRequestService()
70
71         val file = pathProvider.getUniqueFile()
72         file.writeText("Some existing data to send")
73         
74         val sender = StatisticSenderImpl(requestService, pathProvider)
75
76         val isSendFinished = AtomicBoolean(false)
77
78         val lock = Object()
79         ApplicationManager.getApplication().executeOnPooledThread {
80             synchronized(lock, { lock.notify() })
81             sender.sendStatsData("")
82             isSendFinished.set(true)
83         }
84         synchronized(lock, { lock.wait() })
85
86         myFixture.type('.')
87         myFixture.completeBasic()
88         myFixture.type("xx")
89
90         UsefulTestCase.assertFalse(isSendFinished.get())
91     }
92
93     private fun slowRequestService(): RequestService {
94         return mock(RequestService::class.java).apply {
95             `when`(postZipped(anyString(), any() ?: File("."))).then {
96                 Thread.sleep(1000)
97                 ResponseData(200)
98             }
99
100             `when`(post(ArgumentMatchers.anyString(), ArgumentMatchers.anyMap<String, String>())).then {
101                 Thread.sleep(1000)
102                 ResponseData(200)
103             }
104         }
105     }
106
107 }