mamoe / mirai

高效率 QQ 机器人支持库
https://mirai.mamoe.net
GNU Affero General Public License v3.0
14.5k stars 2.55k forks source link

[Mock] 两个小问题 #2548

Closed Nambers closed 1 year ago

Nambers commented 1 year ago

问题描述

  1. resolveFiles 在目标路径没有文件只有文件夹的时候(比如resolveFiles("/a") 但是根目录下只有一个a的文件夹而没有叫a的文件)报错 file not exists:, 期待返回空 flow 我觉得应该在 net.mamoe.mirai.mock.internal.remotefile.absolutefile.MockAbsoluteFolder#resolveFiles 里的第4行添加一个 filter{ isFile } 来解决这个问题, 报错是在这个 map 里面的 toMockAbsFile 抛出

    file not exists: /tx-fs-disk/e005aed8-a488-4b17-95c7-9756a70259ff/4d5cfe46-9694-49c7-ba80-206f402f395c
    java.lang.IllegalStateException: file not exists: /tx-fs-disk/e005aed8-a488-4b17-95c7-9756a70259ff/4d5cfe46-9694-49c7-ba80-206f402f395c
    at net.mamoe.mirai.mock.internal.serverfs.MockServerFileImpl.asExternalResource(MockServerFileDiskImpl.kt:290)
    at net.mamoe.mirai.mock.internal.remotefile.absolutefile.MockAbsoluteFolderKt.toMockAbsFile(MockAbsoluteFolder.kt:51)
    at net.mamoe.mirai.mock.internal.remotefile.absolutefile.MockAbsoluteFolderKt.toMockAbsFile$default(MockAbsoluteFolder.kt:44)
    at net.mamoe.mirai.mock.internal.remotefile.absolutefile.MockAbsoluteFolder$resolveFiles$2.invoke(MockAbsoluteFolder.kt:167)
    at net.mamoe.mirai.mock.internal.remotefile.absolutefile.MockAbsoluteFolder$resolveFiles$2.invoke(MockAbsoluteFolder.kt:166)
  2. mockUploadAudio 报错 RandomAccessFile.inputStream cannot be opened simultaneously., 期待继续上传 原因应该是 net.mamoe.mirai.mock.internal.contact.UtilKt#mockUploadAudio 里的 inResource 调用, 把这个换成 run 之类的就没问题, 可能是里面的 runBIO 和外面的 md5 里面调用 inputStream 冲突/竞争了

    RandomAccessFile.inputStream cannot be opened simultaneously.
    java.lang.IllegalStateException: RandomAccessFile.inputStream cannot be opened simultaneously.
    at net.mamoe.mirai.internal.utils.ExternalResourceImplByFile.inputStream(ExternalResourceImpls.kt:137)
    at net.mamoe.mirai.internal.utils.ExternalResourceImplByFile$md5$2.invoke(ExternalResourceImpls.kt:130)
    at net.mamoe.mirai.internal.utils.ExternalResourceImplByFile$md5$2.invoke(ExternalResourceImpls.kt:130)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at net.mamoe.mirai.internal.utils.ExternalResourceImplByFile.getMd5(ExternalResourceImpls.kt:130)
    at net.mamoe.mirai.mock.internal.contact.UtilKt.mockUploadAudio(util.kt:55)

复现

复制以下复现代码到 mirai-core-mock/test/AbsoluteFileTest.kt

@Test
    @Suppress("INVISIBLE_REFERENCE")
    fun testMockUploadAudio() = runTest {
        val file = withContext(Dispatchers.IO) {
            File.createTempFile("test", ".txt").apply {
                writeText("test")
                deleteOnExit()
            }
        }

        file.toExternalResource().use{
            assertIsInstance<net.mamoe.mirai.internal.utils.ExternalResourceImplByFile>(it)
            // 这个可以正常上传
//            it.mockUploadVoice(bot)
            it.mockUploadAudio(bot)
        }
    }

    @Test
    fun testResolveFiles() = runTest{
        val file = withContext(Dispatchers.IO) {
            File.createTempFile("test", ".txt").apply {
                writeText("test")
                deleteOnExit()
            }
        }
        file.toExternalResource().use {
            group.files.root.uploadNewFile("/a/test.txt", it)
        }
        assertEquals(0, group.files.root.resolveFiles("/a").count())
    }

mirai-core 版本

2.15.0

bot-protocol

ANDROID_PHONE

其他组件版本

No response

系统日志

No response

网络日志

No response

补充信息

No response

Him188 commented 1 year ago

多么优质的 issue, 还把复现 + test 都写好了