小编jos*_*eph的帖子

如何使用 mockito 模拟 grpc ServiceBlockingStub 抛出 StatusRuntimeException(Status.UNAVAILABLE)?

我想模拟我的 grpc 客户端,以确保它通过抛出一个new StatusRuntimeException(Status.UNAVAILABLE)(这是java.net.ConnectException: Connection refused向 grpc 客户端抛出的异常)来应对失败。但是,生成的类是最终的,因此模拟将不起作用。

如何让 BlahServiceBlockingStub 抛出new StatusRuntimeException(Status.UNAVAILABLE)而不必重构我的代码来创建围绕 BlahServiceBlockingStub 的包装类?

这是我尝试过的(其中 BlahServiceBlockingStub 是由 grpc 生成的):

    @Test
    public void test() {
        BlahServiceBlockingStub blahServiceBlockingStub = mock(BlahServiceBlockingStub.class);

        when(blahServiceBlockingStub.blah(any())).thenThrow(new StatusRuntimeException(Status.UNAVAILABLE));


        blahServiceBlockingStub.blah(null);
    }
Run Code Online (Sandbox Code Playgroud)

不幸的是,我按预期得到了以下异常:

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class BlahServiceGrpc$BlahServiceBlockingStub
Mockito cannot mock/spy following:
  - final classes
  - anonymous classes
  - primitive types

    at MyTestClass.test(MyTestClass.java:655)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
.
.
.
Run Code Online (Sandbox Code Playgroud)

因为我尝试模拟 grpc 生成的最终类:

  public static final class BlahServiceBlockingStub extends io.grpc.stub.AbstractStub<BlahServiceBlockingStub> {
    private BlahServiceBlockingStub(io.grpc.Channel channel) { …
Run Code Online (Sandbox Code Playgroud)

java mockito grpc

7
推荐指数
2
解决办法
5316
查看次数

如何在打字稿生成的 JavaScript 中维护空格

我希望从打字稿生成的 JavaScript 代码具有相同的白色间距。长话短说,我们正在从 js 迁移到 ts,并且维护空格将使我们能够将生成的代码与未迁移的代码版本进行比较。

我的输入 ts 看起来像:

id = this.getOrCreate(
    entity.uuid,
    dao.wrap(utils.trimProxyPrefix(entity.name)),
    dao.wrap(entity.type),
    dao.wrap(entity.targetName),
    dao.wrap(entity.targetType),
    dao.wrap(entity.targetIp),
    dao.wrap(host),
    dao.wrap(version),
    dao.wrap(status)
);  
Run Code Online (Sandbox Code Playgroud)

输出看起来很丑陋的超长行:

id = this.getOrCreate(entity.uuid, dao.wrap(utils.trimProxyPrefix(entity.name)), dao.wrap(entity.type), dao.wrap(entity.targetName), dao.wrap(entity.targetType), dao.wrap(entity.targetIp), dao.wrap(host), dao.wrap(version), dao.wrap(status));  
Run Code Online (Sandbox Code Playgroud)

我的预期输出是:

id = this.getOrCreate(
    entity.uuid,
    dao.wrap(utils.trimProxyPrefix(entity.name)),
    dao.wrap(entity.type),
    dao.wrap(entity.targetName),
    dao.wrap(entity.targetType),
    dao.wrap(entity.targetIp),
    dao.wrap(host),
    dao.wrap(version),
    dao.wrap(status)
); 
Run Code Online (Sandbox Code Playgroud)

我的 tsconfig 看起来像:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "./dist",
  },
  "include": ["./src/**/*"],
  "exclude": [
    "node_modules",
    "./src/real/*"
  ]
}
Run Code Online (Sandbox Code Playgroud)

javascript whitespace typescript

6
推荐指数
1
解决办法
2827
查看次数

在Powershell中,如何将二进制文件重定向到标准输入?

这不适用于二进制文件:Redirecting standard input\output in Windows Powershell

以下是我用于输入的内容的摘要。您可以看到 ls 显示文件大小为 858320 字节,但是当通过 Get-Content 进行管道传输时,情况并非如此。

PS C:\Users\Joseph\Documents\GitHub\java_hprof_to_txt> ls .\generate_hprof\heap.dump.hprof


    Directory: C:\Users\Joseph\Documents\GitHub\java_hprof_to_txt\generate_hprof


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/28/2017  12:02 PM         858320 heap.dump.hprof
Run Code Online (Sandbox Code Playgroud)

这是我的最小测试程序,它计算标准输入中的字节数,直到达到 eof:

#include <stdio.h>  
#include <fcntl.h>  
#include <io.h> 


int main()
{
    char buff;
    int numOfBytesRead;
    int dataLen = 0;

    #ifdef _WIN32
    int result = _setmode(_fileno(stdin), _O_BINARY);
    if (result == -1)
        perror("Cannot set mode");
    else
        printf("'stdin' successfully changed to binary mode\n");
    #endif

    while (true) {
        numOfBytesRead = …
Run Code Online (Sandbox Code Playgroud)

powershell

5
推荐指数
1
解决办法
2144
查看次数

为什么最近 git rebase -i squash 导致头部分离

为什么 git rebase -i 最近会导致头部分离?它曾经用交互式变基的结果来更新我当前的分支。如何获得交互式变基以停止进入分离的 HEAD?

在从远程存储库拉取之前,我总是使用 git rebase -i 来压缩我的提交,以简化处理 git pull 中的任何合并冲突。我不必解决可能多个提交的冲突,而只需解决一个提交。

我使用的示例命令

# git rebase -i <tip of public branch> <my latest commit>
git rebase -i 380647533da 82f5ee67bed
Run Code Online (Sandbox Code Playgroud)

在 vim 中编辑 rebase 交互后:

pick 3ec7c211c49 version1
s 82f5ee67bed some significant refactoring
Run Code Online (Sandbox Code Playgroud)

编辑并保存提交后的输出:

[detached HEAD ea50304796c] version1
 Date: Thu Jun 6 17:04:36 2019 -0400
 14 files changed, 213 insertions(+), 52 deletions(-)
 create mode 100644     some file
 create mode 100644     some file
 create mode 100644     some file
Successfully rebased …
Run Code Online (Sandbox Code Playgroud)

git git-interactive-rebase git-detached-head

5
推荐指数
1
解决办法
2522
查看次数

如何从堆转储中提取时间戳

不幸的是,我忘记记录进行堆转储的时间。我希望标准库在堆中的某个地方缓存类似System.currentTimeMillis(). 不幸的是,我没有任何缓存它的业务对象。

一个困难的选择是浏览所有线程,并查看它们的局部变量是否在某处存储了时间戳。但是,这不是我可以应用于所有堆转储的技术。

我查看了 openJDK 中的 java.lang.System,看起来我们没有缓存值。我们使用本机代码来获取当前时间,但我们不会将本机代码的结果存储在任何地方。https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/4d891c8db5c1/src/share/classes/java/lang/System.java

hprof heap-dump

4
推荐指数
1
解决办法
1825
查看次数

Apache mina-sshd ssh 客户端总是打印 EdDSA 提供程序不支持

我正在使用 Apache sshd 的 ssh 客户端。每当我建立与目标 ssh 服务器的连接时,我都会在日志中看到这一点。连接正常,但是有什么问题吗?我该如何修复它?

异常看起来像:

(SshException) to process: EdDSA provider not supported
Run Code Online (Sandbox Code Playgroud)

java ssh apache-mina

3
推荐指数
1
解决办法
6366
查看次数

如何告诉摘要不要换行?

我正在尝试获取摘要以不包装其输出。但是,当我使用 5 列数据调用摘要时,它将第 5 列放在单独的行上。我希望有一种方法比手动打印迭代摘要返回的对象更容易。

此致,

约瑟夫

绘图程序

#!/usr/bin/Rscript
noneData <- read.csv("query.log", header=FALSE, sep="\t");
cnames = c( 'vids', 'partitions', 'localvert', 'remotevert',
                    'cachehits', 'responsetime' );
colnames(noneData) <- cnames;
cachenames = c('1', '2', '3', '4', '5');
responseCompare = data.frame(
   noneData$responsetime/1000000,
   noneData$responsetime/1000000,
   noneData$responsetime/1000000,
   noneData$responsetime/1000000,
   noneData$responsetime/1000000
   );
colnames(responseCompare) <- cachenames;
print("Response Times");
summary(responseCompare);
Run Code Online (Sandbox Code Playgroud)

./plotit.r #输出:

[1] "Response Times"
       1                  2                  3                  4         
 Min.   :   0.215   Min.   :   0.215   Min.   :   0.215   Min.   :   0.215
 1st Qu.: 395.202   1st Qu.: 395.202   1st Qu.: 395.202   1st …
Run Code Online (Sandbox Code Playgroud)

r rscript

2
推荐指数
1
解决办法
1974
查看次数

如何将VisualVM OQL结果直接保存到文件中?

我的查询结果有数百万行。我已经修改了visualvm.conf

-J-DOQLController.limitResults=1000000
Run Code Online (Sandbox Code Playgroud)

目前,作为解决方法,我运行查询,并将结果复制并粘贴到文件中。然而,结果给我的记忆带来了很大的压力。是否有办法跳过将结果显示到 UI,而直接将结果流式传输到文件?

我的查询如下所示:

select { obj1: busObj.obj1, ... , objN: busObj.objN }
  from com.my.BusinessObject busObj
Run Code Online (Sandbox Code Playgroud)

visualvm oql

2
推荐指数
1
解决办法
1095
查看次数

双重时间单位转换

当结果> 1时,TimeUnit.toSeconds效果很好。但是,由于我们要处理long,因此如果结果介于0和1之间(不包括),我们将得到0。一个Java SDK函数可以为我们处理双重转换吗?

我知道我可以自己乘以或除以适当的两倍来完成这些转换。但是,它容易出错,并且不能很好地阅读。我更喜欢使用Java SDK中的现有功能,类似于Java的TimeUnit。

展示我的情况的最小示例:

import java.util.*;
import java.util.concurrent.*;
public class Main {
    public static void main(String[] args) {
        System.out.println(TimeUnit.MILLISECONDS.toSeconds(1));
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

0
Run Code Online (Sandbox Code Playgroud)

我想要一个处理双精度函数并返回0.001的函数

java timeunit

1
推荐指数
1
解决办法
619
查看次数

如何在 perl 中从 &lt;STDIN&gt; 嵌套读取?

我正在编写一个脚本来解析来自 Java 的线程转储。出于某种原因,当我尝试从子例程内或嵌套循环内读取时,它根本不进入嵌套循环。理想情况下,我希望能够在嵌套循环上对 STDIN 进行操作,否则您将不得不编写一些丑陋的状态转换代码。

在我使用 STDIN 之前,但只是为了确保我的子程序没有指向 STDIN 的独立指针,我将它打开到$in.

当我运行它时,它看起来像下面。您可以看到它永远不会进入嵌套循环,尽管外循环有更多来自 STDIN 的文件要读取。

~/$ cat catalina.out-20160* | thread.dump.find.all.pl
in is GLOB(0x7f8d440054e8)
found start of thread dump at 2016-06-17 13:38:23 saving to tdump.2016.06.17.13.38.23.txt
in is GLOB(0x7f8d440054e8)
BEFORE NESTED STDIN
BUG!!!!
found start of thread dump at 2016-06-17 13:43:05 saving to tdump.2016.06.17.13.43.05.txt
in is GLOB(0x7f8d440054e8)
BEFORE NESTED STDIN
BUG!!!!
...
Run Code Online (Sandbox Code Playgroud)

编码:

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use DateTime::Format::Strptime;
use DateTime::Format::Duration;
use Data::Dumper;
# DO NOT touch ARGV!
Getopt::Long::Configure("pass_through"); …
Run Code Online (Sandbox Code Playgroud)

perl stdin

0
推荐指数
1
解决办法
229
查看次数