2 * Copyright 2000-2017 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.stats.completion
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
29 import java.util.concurrent.atomic.AtomicBoolean
32 class PerformanceTests : LightFixtureCompletionTestCase() {
33 private lateinit var pathProvider: FilePathProvider
35 private val runnable = "interface Runnable { void run(); void notify(); void wait(); void notifyAll(); }"
36 private val text = """
39 Runnable r = new Runnable() {
47 override fun setUp() {
49 val container = ApplicationManager.getApplication().picoContainer as MutablePicoContainer
50 pathProvider = container.getComponentInstance(FilePathProvider::class.java.name) as FilePathProvider
51 CompletionTrackerInitializer.isEnabledInTests = true
54 override fun tearDown() {
55 CompletionTrackerInitializer.isEnabledInTests = false
59 CompletionLoggerProvider.getInstance().dispose()
60 val statsDir = pathProvider.getStatsDataDirectory()
61 statsDir.deleteRecursively()
65 fun `test do not block EDT on data send`() {
66 myFixture.configureByText("Test.java", text)
67 myFixture.addClass(runnable)
69 val requestService = slowRequestService()
71 val file = pathProvider.getUniqueFile()
72 file.writeText("Some existing data to send")
74 val sender = StatisticSenderImpl(requestService, pathProvider)
76 val isSendFinished = AtomicBoolean(false)
79 ApplicationManager.getApplication().executeOnPooledThread {
80 synchronized(lock, { lock.notify() })
81 sender.sendStatsData("")
82 isSendFinished.set(true)
84 synchronized(lock, { lock.wait() })
87 myFixture.completeBasic()
90 UsefulTestCase.assertFalse(isSendFinished.get())
93 private fun slowRequestService(): RequestService {
94 return mock(RequestService::class.java).apply {
95 `when`(postZipped(anyString(), any() ?: File("."))).then {
100 `when`(post(ArgumentMatchers.anyString(), ArgumentMatchers.anyMap<String, String>())).then {