2d06ca5bd7f0269854f85c2aa0754565a9be2896
[idea/community.git] / plugins / stats-collector / test / com / intellij / stats / completion / storage / StorageTest.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.storage
17
18 import com.intellij.openapi.util.text.StringUtil
19 import com.intellij.stats.logger.LineStorage
20 import com.intellij.stats.logger.LogFileManager
21 import com.intellij.stats.storage.UniqueFilesProvider
22 import org.assertj.core.api.Assertions.assertThat
23 import org.junit.After
24 import org.junit.Before
25 import org.junit.Test
26 import java.io.File
27
28
29 class FilesProviderTest {
30     
31     lateinit var provider: UniqueFilesProvider
32
33     @Before
34     fun setUp() {
35         provider = UniqueFilesProvider("chunk", ".", "logs-data")
36         provider.getStatsDataDirectory().deleteRecursively()
37     }
38
39     @After
40     fun tearDown() {
41         provider.getStatsDataDirectory().deleteRecursively()
42     }
43     
44     @Test
45     fun test_three_new_files_created() {
46         provider.getUniqueFile().createNewFile()
47         provider.getUniqueFile().createNewFile()
48         provider.getUniqueFile().createNewFile()
49         
50         val createdFiles = provider.getDataFiles().count()
51         
52         assertThat(createdFiles).isEqualTo(3)
53     }
54 }
55
56
57 class AsciiMessageStorageTest {
58     
59     lateinit var storage: LineStorage
60     lateinit var tmpFile: File
61
62     @Before
63     fun setUp() {
64         storage = LineStorage()
65         tmpFile = File("tmp_test")
66         tmpFile.delete()
67     }
68
69     @After
70     fun tearDown() {
71         tmpFile.delete()
72     }
73
74     @Test
75     fun test_size_with_new_lines() {
76         val line = "text"
77         storage.appendLine(line)
78         assertThat(storage.sizeWithNewLine("")).isEqualTo(line.length + 2 * System.lineSeparator().length)
79     }
80     
81     @Test
82     fun test_size_is_same_as_file_size() {
83         val line = "text"
84         storage.appendLine(line)
85         storage.appendLine(line)
86
87         val expectedSize = 2 * (line.length + System.lineSeparator().length)
88         assertThat(storage.size).isEqualTo(expectedSize)
89
90         storage.dump(tmpFile)
91         assertThat(tmpFile.length()).isEqualTo(expectedSize.toLong())
92     }
93     
94     
95 }
96
97
98
99 class FileLoggerTest {
100     
101     lateinit var fileLogger: LogFileManager
102     lateinit var filesProvider: UniqueFilesProvider
103
104     @Before
105     fun setUp() {
106         filesProvider = UniqueFilesProvider("chunk", ".", "logs-data")
107         val dir = filesProvider.getStatsDataDirectory()
108         dir.deleteRecursively()
109         fileLogger = LogFileManager(filesProvider)
110     }
111
112     @After
113     fun tearDown() {
114         val dir = filesProvider.getStatsDataDirectory()
115         dir.deleteRecursively()
116     }
117
118     @Test
119     fun test_chunk_is_around_256Kb() {
120         val bytesToWrite = 1024 * 200
121         val text = StringUtil.repeat("c", bytesToWrite)
122         fileLogger.println(text)
123         fileLogger.dispose()
124
125         val chunks = filesProvider.getDataFiles()
126         assertThat(chunks).hasSize(1)
127
128         val fileLength = chunks.first().length()
129         assertThat(fileLength).isLessThan(256 * 1024)
130         assertThat(fileLength).isGreaterThan(200 * 1024)
131     }
132     
133     @Test
134     fun test_multiple_chunks() {
135         writeKb(1024)
136
137         val files = filesProvider.getDataFiles()
138         val fileIndexes = files.map { it.name.substringAfter('_').toInt() }
139         assertThat(files.isNotEmpty()).isTrue()
140         assertThat(fileIndexes).isEqualTo((0..files.size - 1).toList())
141     }
142
143
144     @Test
145     fun test_delete_old_stuff() {
146         writeKb(4096)
147
148         var files = filesProvider.getDataFiles()
149
150         val totalSize = files.fold(0L, { total, file -> total + file.length() })
151         assertThat(totalSize > 2 * 1024 * 1024).isTrue()
152
153         val firstBefore = files
154           .map { it.name.substringAfter('_').toInt() }
155           .sorted()
156           .first()
157         
158         assertThat(firstBefore).isEqualTo(0)
159         
160         filesProvider.cleanupOldFiles()
161         files = filesProvider.getDataFiles()
162
163         val totalSizeAfterCleanup = files.fold(0L, { total, file -> total + file.length() })
164         assertThat(totalSizeAfterCleanup < 2 * 1024 * 1024).isTrue()
165
166         val firstAfter = files
167           .map { it.name.substringAfter('_').toInt() }
168           .sorted()
169           .first()
170         
171         assertThat(firstAfter).isGreaterThan(0)
172     }
173
174     private fun writeKb(kb: Int) {
175         val lineLength = System.lineSeparator().length
176         (0..kb * 1024 / lineLength).forEach {
177             fileLogger.println("")
178         }
179         fileLogger.dispose()
180     }
181
182 }